title: c# - How to iterate through classes fields and set properties
body: <p>I am not sure if this is possible but I want to iterate through a class and set a field member property without referring to the field object explicitly:</p>
  
  <pre><code>public class Employee
  {
    public Person _person = new Person();
  
    public void DynamicallySetPersonProperty()
    {
      MemberInfo[] members = this.GetType().GetMembers();
  
      foreach (MemberInfo member in members.Where(a =&gt; a.Name == "_person"))
      //get the _person field
      {
  
        Type type = member.GetType();
        PropertyInfo prop = type.GetProperty("Name"); //good, this works, now to set a value for it
  
        //this line does not work - the error is "property set method not found"
        prop.SetValue(member, "new name", null);
      }
    }
  }
  
  
  public class Person
  {
    public string Name { get; set; }
  }
  </code></pre>
  
  <p>In the answer that I marked as the answer you need to add:</p>
  
  <pre><code>  public static bool IsNullOrEmpty(this string source)
    {
      return (source == null || source.Length &gt; 0) ? true : false;
    }
  </code></pre>