39

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.

Thanks for your help.

Answer:

Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName");
PropertyInfo _propertyInfo = _type.GetProperty("Field1");
_propertyInfo.SetValue(_type, _newValue, null);
Community
  • 1
  • 1
iefpw
  • 6,816
  • 15
  • 55
  • 79

3 Answers3

48

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); 
  } 
Sofian Hnaide
  • 2,284
  • 16
  • 13
27

you can get the PropertyInfo like this and set the value

var propertyInfo=obj.GetType().GetProperty(propertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
propertyInfo.SetValue(obj, value,null);
Beatles1692
  • 5,214
  • 34
  • 65
  • I've created a couple of methods to save and load app settings, that in-app are a static class, using Reflection. Thx. See http://www.sportronics.com.au/csharp/Savings-and-Loading-Application-Settings-through-Refection-csharp.html – David Jones Aug 13 '19 at 13:44
  • 1
    @DavidJones. I am getting a 404 – Beatles1692 Aug 17 '19 at 11:39
  • Sorry about that. Try this: http://www.sportronics.com.au/appdev/Savings-and-Loading-Application-Settings-through-Refection-appdev.html (Updated) – David Jones Aug 18 '19 at 14:26
2

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

}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135