0

我尝试List<>从动态类中生成一个,在这里的一个人的帮助下,我走得更远了,但不是我需要的

我需要这个:imgur.com/OIiYQ0f

我有这个:imgur.com/9shrhu6

无论我尝试什么,我似乎都无法获得正确的输出,也不明白我所拥有的和应该拥有的(名称的键)的区别,如果有人可以解决这个问题并解释有什么区别我的代码会很棒:

foreach (DataRow r in _data.Rows)
{
DynamicClass dynamicClass = new DynamicClass();

foreach (String Property in Properties)
{
    dynamicClass.Property[Property.ToString()] = _data.Rows[i][Property].ToString(); 
}
    DynamicClassList.Add(dynamicClass);
}

[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,nq}")]
    private class KeyValuePair
    {
        private object 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;
            }
        }
    }
}
4

0 回答 0