可能重复:
.Net - Reflection set object property
Setting a property by reflection with a string valueC#动态设置属性
我敢肯定有这样做的一个简单的方法,我是厚,但我可以”琢磨出我的生活。
我有一个对象具有多个属性。我们调用对象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'传递。 – James
的确,我确实是指ref,是的,我打算改变实际的实例。 –