【发布时间】:2012-10-09 20:06:48
【问题描述】:
可能重复:
.Net - Reflection set object property
Setting a property by reflection with a string value
我有一个具有多个属性的对象。我们称对象为 objName。我正在尝试创建一个简单地使用新属性值更新对象的方法。
我希望能够在一个方法中做到以下几点:
private void SetObjectProperty(string propertyName, string value, ref object objName)
{
//some processing on the rest of the code to make sure we actually want to set this value.
objName.propertyName = value
}
最后是调用:
SetObjectProperty("nameOfProperty", textBoxValue.Text, ref objName);
希望这个问题足够充实。如果您需要更多详细信息,请告诉我。
感谢大家的回答!
【问题讨论】:
-
@DavidArcher C# 中没有
rel键盘...我认为您的意思是ref?除非您打算更改它的实际实例,否则无需将对象作为ref传递。 -
确实,我的意思是 ref,是的,我确实打算更改实际实例。