title: Set property value using property name
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7718792/can-i-set-a-property-value-with-reflection">Can I set a property value with Reflection?</a>  </p>
  </blockquote>
  
  
  
  <p>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:</p>
  
  <pre><code>List&lt;KeyValuePair&lt;string, object&gt;&gt; _lObjects = GetObjectsList();
  
  foreach(KeyValuePair&lt;string, object&gt; _pair in _lObjects) 
  {
    //class have this static property name stored in _pair.Key
    Class1.[_pair.Key] = (cast using typeof (_pair.Value))_pair.Value;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks for your help.</p>
  
  <p>Answer:</p>
  
  <pre><code>Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName");
  PropertyInfo _propertyInfo = _type.GetProperty("Field1");
  _propertyInfo.SetValue(_type, _newValue, null);
  </code></pre>