我有一个样本类更新使用反射和属性的名称的类属性作为字符串
public class sampleClass
{
public string givenName { get; set; }
public string familyName { get; set; }
}
和对于包含在IDictionary<string, object> dataModel
该类的一组值。我可以使用反射遍历dataModel
并使用dataModel
键获取值。
我想这样做:
void UpdateValues(IDictionary<string, object> dataModel)
{
Type sourceType = typeof(sampleClass);
foreach (PropertyInfo propInfo in (sourceType.GetProperties()))
{
if (dataModel.ContainsKey(propInfo.Name))
{
// set propInfo value here
propInfo.Value = dataModel[propInfo.Name];
}
}
}
,但我不知道怎么做线
propInfo.Value = dataModel[propInfo.Name];
帮助!谢谢 !!
非常感谢,没很好的诀窍。 –