C# 使用属性名称设置属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9404523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set property value using property name
提问by iefpw
Possible Duplicate:
Can I set a property value with Reflection?
可能的重复:
我可以使用反射设置属性值吗?
How do I set a static property of a class using reflection when I only have its string name of the property? For instance I have:
当我只有属性的字符串名称时,如何使用反射设置类的静态属性?例如我有:
List<KeyValuePair<string, object>> _lObjects = GetObjectsList();
foreach(KeyValuePair<string, object> _pair in _lObjects)
{
//class have this static property name stored in _pair.Key
Class1.[_pair.Key] = (cast using typeof (_pair.Value))_pair.Value;
}
I don't know how I should set the value of the property using the property name string. Everything is dynamic. I could be setting 5 static properties of a class using 5 items in the list that each have different types.
我不知道应该如何使用属性名称字符串设置属性的值。一切都是动态的。我可以使用列表中的 5 个项目设置一个类的 5 个静态属性,每个项目都有不同的类型。
Thanks for your help.
谢谢你的帮助。
Answer:
回答:
Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName");
PropertyInfo _propertyInfo = _type.GetProperty("Field1");
_propertyInfo.SetValue(_type, _newValue, null);
采纳答案by Sofian Hnaide
You can try something like this
你可以试试这样的
List<KeyValuePair<string, object>> _lObjects = GetObjectsList();
var class1 = new Class1();
var class1Type = typeof(class1);
foreach(KeyValuePair<string, object> _pair in _lObjects)
{
//class have this static property name stored in _pair.Key
class1Type.GetProperty(_pair.Key).SetValue(class1, _pair.Value);
}
回答by Beatles1692
you can get the PropertyInfo like this and set the value
您可以像这样获取 PropertyInfo 并设置值
var propertyInfo=obj.GetType().GetProperty(propertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
propertyInfo.SetValue(obj, value,null);
回答by Nicholas Carey
Like this:
像这样:
class Widget
{
static Widget()
{
StaticWidgetProperty = int.MinValue ;
return ;
}
public Widget( int x )
{
this.InstanceWidgetProperty = x ;
return ;
}
public static int StaticWidgetProperty { get ; set ; }
public int InstanceWidgetProperty { get ; set ; }
}
class Program
{
static void Main()
{
Widget myWidget = new Widget(-42) ;
setStaticProperty<int>( typeof(Widget) , "StaticWidgetProperty" , 72 ) ;
setInstanceProperty<int>( myWidget , "InstanceWidgetProperty" , 123 ) ;
return ;
}
static void setStaticProperty<PROPERTY_TYPE>( Type type , string propertyName , PROPERTY_TYPE value )
{
PropertyInfo propertyInfo = type.GetProperty( propertyName , BindingFlags.Public|BindingFlags.Static , null , typeof(PROPERTY_TYPE) , new Type[0] , null ) ;
propertyInfo.SetValue( null , value , null ) ;
return ;
}
static void setInstanceProperty<PROPERTY_TYPE>( object instance , string propertyName , PROPERTY_TYPE value )
{
Type type = instance.GetType() ;
PropertyInfo propertyInfo = type.GetProperty( propertyName , BindingFlags.Instance|BindingFlags.Public , null , typeof(PROPERTY_TYPE) , new Type[0] , null ) ;
propertyInfo.SetValue( instance , value , null ) ;
return ;
}
}

