在 C# 中,我有一个类“CItems”的多个实例化(见下文)。我在我想使用的实例化的运行时检索一个字符串(在这种情况下调用公共方法-“addPropertyToList”)。我知道我必须使用反射,但我似乎无法正确处理。
CItems me = new CItems();
CItems conversations = new CItems();
string whichCItem = "me"
properties = <whichCItem>.addPropertyToList(properties, "FirstName", "Ken");
我尝试了很多事情,例如:
var myobject = this;
string propertyname = "me";
PropertyInfo property = myobject.GetType().GetProperty(propertyname);
object value = property.GetValue(myobject, null);
但这导致:对象引用未设置为对象的实例。因为属性最终为空。
感谢您的帮助,请保持温柔。我真的不知道我在做什么,我可能使用了一些错误的术语。