C#动态设置属性 [英] C# dynamically set property
本文介绍了C#动态设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
的网络 - 反射设置对象属性结果
的设置反射属性具有字符串值
我敢肯定有这样做的一个简单的方法,我是厚,但我想不出它为我的生活。
I'm sure there's a simple way of doing this and I'm being thick, but I can't figure it out for the life of me.
我有多个属性的对象。让我们来调用对象OBJNAME。 。我试图创建一个简单的更新与新的属性值对象的方法
I have an object with multiple properties. Let's call the object objName. I'm trying to create a method that simply updates the object with the new property values.
我希望能够做的方法如下:
I want to be able to do the following in a method:
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);
希望的问题是充实就够了。让我知道如果你需要更多的细节。
Hope the question is fleshed out enough. Let me know if you need more details.
感谢您的答案大家!
推荐答案
objName.GetType()的getProperty(nameOfProperty)。的SetValue(OBJNAME,objValue,空)
这篇关于C#动态设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文