如何使用反射在 winforms c# 中为字段设置值
How to Set Value for Field in winforms c# using Reflection
我的项目有这个:
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);
有什么想法吗?
谢谢!
也许:
f.FieldType.GetProperty("ID").SetValue(this,"ERROR",null);
我有我使用的答案 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);
我的项目有这个:
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);
有什么想法吗?
谢谢!
也许:
f.FieldType.GetProperty("ID").SetValue(this,"ERROR",null);
我有我使用的答案 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);