在列表<>中获取属性值而不是[0],[1] [英] Get property value in List<> instead of [0],[1]

查看:272
本文介绍了在列表<>中获取属性值而不是[0],[1]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个动态类来生成列表,但是在属性中; ist我不会得到名字,因为你得到一个静态类,但是[0],和一些专门的类。

  public class Program 
{
private static void Main(string [] args)
{
DynamicClass dynamicClass = new DynamicClass();
dynamicClass.Property [Test] = 1;
dynamicClass.Property [test2] =foo;

Debugger.Break();
}
}


[DebuggerTypeProxy(typeof(DynamicClassProxy))]
public class DynamicClass
{
// property是一个类,将在运行时创建动态属性
private DynamicProperty _property = new DynamicProperty();

public DynamicProperty属性
{
get {return _property; }
set {_property = value; }
}

[DebuggerDisplay({value},Name ={key})]
private class KeyValuePair
{
private string键;
私有对象值;

public KeyValuePair(KeyValuePair< string,object> kvp)
{
this.value = kvp.Value;
this.key = kvp.Key;
}
}

私有类DynamicClassProxy
{
private DynamicClass _dynamicClass;
public DynamicClassProxy(DynamicClass dynamicClass)
{
_dynamicClass = dynamicClass;
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePair [] Keys
{
get
{
return _dynamicClass.Property.properties.Select(x => new KeyValuePair(x))。ToArray();
}
}
}

public class DynamicProperty
{
//使它没有人可以创建这些对象。
内部DynamicProperty(){}

//一个包含所有动态属性值的字典
内部字典< string,object> properties = new Dictionary< string,object>();

//在我们的Dictionary中获取任何动态属性的属性调用,如果没有找到,则返回。
public object this [string name]
{
get
{
if(properties.ContainsKey(name))
{
返回属性[名称];
}
return;
}
set
{
属性[name] = value;
}
}
}
}






更新:要将密钥名称周围的引号添加到 nq 名称属性。

  [DebuggerDisplay({value},Name ={key,nq })] 
private class KeyValuePair
{
private object key;
私有对象值;

public KeyValuePair(KeyValuePair< string,object> kvp)
{
this.value = kvp.Value;
this.key = kvp.Key;
}
}


I am using a dynamic class to generate a list, however in the property ;ist I dont get the names as you get in a static class but [0],1,[2], ect. I need these names into the properties.

Anybody knows the (probably) simple answer?

here my code

UPDATE 1:

List<string> Properties = new List<string>();
        Properties.Add("name");
        Properties.Add("instituteTypeId");
        Properties.Add("city");
        Properties.Add("id");

List<DynamicClass> DynamicClassList = new List<DynamicClass>();

            int i = 0;
           foreach (DataRow r in _data.Rows)
           {
               DynamicClass dynamicClass = new DynamicClass();
            foreach (String Property in Properties)
              {
                dynamicClass.property[Property.ToString()] = _data.Rows[i][Property].ToString(); // private string RandomString(int size)
            }
            DynamicClassList.Add(dynamicClass);
}

My dynamic class is:

public class DynamicClass
{
    // property is a class that will create dynamic properties at runtime
    private DynamicProperty _property = new DynamicProperty();

    public DynamicProperty property
    {
        get { return _property; }
        set { _property = value; }
    }
}
public class DynamicProperty
{
    // a Dictionary that hold all the dynamic property values
    private Dictionary<string, object> properties = new Dictionary<string, object>();

    // the property call to get any dynamic property in our Dictionary, or "" if none found.
    public object this[string name]
    {
        get
        {
            if (properties.ContainsKey(name))
            {
                return properties[name];
            }
            return "";
        }
        set
        {
            properties[name] = value;
        }
    }
}

Which should give me the same result as:

Medical data = new Medical();
data.Name = _data.Rows[i]["name"].ToString();
data.instituteTypeId = Convert.ToInt32(_data.Rows[i]["instituteTypeId"].ToString());
data.City = _data.Rows[i]["city"].ToString();
data.ID = Convert.ToInt32(_data.Rows[i]["id"].ToString());
list.Add(data);

UPDATE 2:

public class Medical
{
    public string Name { get; set; }
    public int ID { get; set; }
    public string City { get; set; }
    public int instituteTypeId { get; set; }
}

If I look to the property on runtime from the DynamicClassList I see with the dynamic (sorry dont know how to upload image) something like this:

key   value
[0]   [{id,1}]
[1]   [{name, Med 1}]

While in the static Class it does correct as I need

key   value
[id]  {1}
[name] {Med 1}

From my point of view they are the same with the slide difference that with static I see the key values in the key field and with the dynamic in a [{key,value}]

Any help will be appreciated

解决方案

You need to use a DebuggerTypeProxy and a few specialized classes.

public class Program
{
    private static void Main(string[] args)
    {
        DynamicClass dynamicClass = new DynamicClass();
        dynamicClass.Property["Test"] = 1;
        dynamicClass.Property["test2"] = "foo";

        Debugger.Break();
    }
}


[DebuggerTypeProxy(typeof(DynamicClassProxy))]
public class DynamicClass
{
    // property is a class that will create dynamic properties at runtime
    private DynamicProperty _property = new DynamicProperty();

    public DynamicProperty Property
    {
        get { return _property; }
        set { _property = value; }
    }

    [DebuggerDisplay("{value}", Name = "{key}")]
    private class KeyValuePair
    {
        private string key;
        private object value;

        public KeyValuePair(KeyValuePair<string,object> kvp)
        {
            this.value = kvp.Value;
            this.key = kvp.Key;
        }
    }

    private class DynamicClassProxy
    {
        private DynamicClass _dynamicClass;
        public DynamicClassProxy(DynamicClass dynamicClass)
        {
            _dynamicClass = dynamicClass;
        }

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public KeyValuePair[] Keys
        {
            get
            {
                return _dynamicClass.Property.properties.Select(x => new KeyValuePair(x)).ToArray();
            }
        }
    }

    public class DynamicProperty
    {
        //Make it so no one can create these objects.
        internal DynamicProperty() { }

        // a Dictionary that hold all the dynamic property values
        internal Dictionary<string, object> properties = new Dictionary<string, object>();

        // the property call to get any dynamic property in our Dictionary, or "" if none found.
        public object this[string name]
        {
            get
            {
                if (properties.ContainsKey(name))
                {
                    return properties[name];
                }
                return "";
            }
            set
            {
                properties[name] = value;
            }
        }
    }
}


UPDATE: To remote the quotes around the key name add nq to the Name property.

[DebuggerDisplay("{value}", Name = "{key,nq}")]
private class KeyValuePair
{
    private object key;
    private object value;

    public KeyValuePair(KeyValuePair<string,object> kvp)
    {
        this.value = kvp.Value;
        this.key = kvp.Key;
    }
}

这篇关于在列表&lt;&gt;中获取属性值而不是[0],[1]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆