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); 
                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);    }  
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With