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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…