在List<>中获取属性值而不是[0],[1]

时间:2013-10-07 14:44:21

标签: c# list dynamic

我正在使用动态类来生成列表,但是在属性中; ist我得不到静态类中的名称,但是[0],1,[2]等。 我需要在属性中使用这些名称。

有人知道(可能)简单的答案吗?

这里是我的代码

更新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);
}

我的动态课程是:

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;
        }
    }
}

哪个应该给我相同的结果:

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);

更新2:

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

如果我从DynamicClassList中查看运行时的属性,我看到动态(抱歉不知道如何上传图像)这样的事情:

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

在静态类中,它确实正确,因为我需要

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

enter image description here

从我的观点来看,它们与幻灯片的区别是相同的,与静态我看到关键字段中的关键值以及[{key,value}]中的动态

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您需要使用DebuggerTypeProxy和一些专门的课程。

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;
            }
        }
    }
}

更新:要远离键名称周围的引号,请将nq添加到Name属性。

[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;
    }
}

enter image description here