我尝试从一个动态类中生成一个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;
}
}
}
}