2012-02-22 18 views
16

Duplicar posible:
Can I set a property value with Reflection?Establecer valor de la propiedad nombre utilizando la propiedad

¿Cómo se configura una propiedad estática de una clase utilizando la reflexión cuando sólo tengo su nombre cadena de la propiedad? Por ejemplo, tengo:

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; 
} 

No sé cómo debo establecer el valor de la propiedad usando la cadena del nombre de la propiedad. Todo es dinámico Podría establecer 5 propiedades estáticas de una clase usando 5 elementos en la lista que tienen diferentes tipos.

Gracias por su ayuda.

Respuesta:

Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName"); 
PropertyInfo _propertyInfo = _type.GetProperty("Field1"); 
_propertyInfo.SetValue(_type, _newValue, null); 

Respuesta

16

Usted puede intentar algo como esto

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); 
    } 
9

se puede obtener el PropertyInfo como este y establecer el valor

var propertyInfo=obj.GetType().GetProperty(propertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); 
propertyInfo.SetValue(obj, value,null); 
1

De esta manera:

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 ; 
    } 

} 
Cuestiones relacionadas