Is there any way to do this? I try to test if a property of an object exists and if it does, I want to set a value to it. (Maybe the complete idea is bad, if true - why?)
class Info
{
public string X1{ set; get; }
public string X2{ set; get; }
public string X3{ set; get; }
}
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("X1","blah1");
values.Add("X2","blah2");
values.Add("NotThere","blah3");
Info info = new Info();
foreach (var item in values)
{
string propertyName = item.Key;
string value = item.Value;
if (info.GetType().GetProperty(propertyName) != null) //this probably works
{
info.propertyName = value; //this doesn't, how to set it?
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…