如何使用Reflection在winforms c#中设置字段的值

时间:2016-07-05 14:39:52

标签: c# winforms reflection setvalue

我的项目有这个:

private clsDTO_Error DTO_Error; --> Have PropertyName = ID
private clsDTO_Right DTO_Right;
FieldInfo f = this.GetType().GetField(DTO_Error, BindingFlags.NonPublic | BindingFlags.Instance);

此示例正常运行

  

DTO_Right.GetType()。GetProperty(PropertyName)。SetValue(DTO_Right,“OK”,null);

  

f.FieldType.GetProperty( “ID”)的SetValue(DTO_Error, “OK”,NULL);

我有一个问题:

  

f.FieldType.GetProperty( “ID”)的SetValue(f.FieldType, “ERROR”,NULL);

对此有何想法?

谢谢!

2 个答案:

答案 0 :(得分:0)

也许:

f.FieldType.GetProperty(" ID&#34)的SetValue(此," ERROR",NULL);

答案 1 :(得分:0)

我的答案是我使用Directionary

private Dictionary<string, object> myDictionary = new Dictionary<string, object>();
//Add this in Form Load
myDictionary.Add("DTO_Error", DTO_Error);
//Set Value
PropertyInField.SetValue(myDictionary[DTO_Error], "OK", null);