title: C# Populating Objects with PropertyInfo
body: <p>I have the following classes:</p>
  
  <p>User</p>
  
  <pre><code>public partial class User
  {
      public long iduser { get; set; }
      public string email { get; set; }
      public string name { get; set; }
      public System.DateTime birthdate { get; set; }
      public string about { get; set; }
      public bool active { get; set; }
      public System.DateTime created_date { get; set; }
      public System.DateTime last_update { get; set; }
      public string password { get; set; }
      public string image { get; set; }
      public string username { get; set; }
      public virtual ICollection&lt;Interest&gt; Interests { get; set; }
  }
  </code></pre>
  
  <p>Interest</p>
  
  <pre><code>public partial class Interest
  {
  
      public long idinterest { get; set; }
      public string name { get; set; }
      public bool active { get; set; }
      public System.DateTime last_update { get; set; }
      public System.DateTime created_date { get; set; }
      public string css_class { get; set; }
  
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p>WSReturnUserGetById</p>
  
  <pre><code>public class WSReturnUserGetById
  {
      public long iduser { get; set; }
      public string email { get; set; }
      public string name { get; set; }
      public System.DateTime birthdate { get; set; }
      public string about { get; set; }
      public List&lt;WSReturnInterestGetById&gt; Interests { get; set; }
  }
  </code></pre>
  
  <p>and WSReturnInterestBetById</p>
  
  <pre><code>public class WSReturnInterestGetById
  {
      public long idinterest { get; set; }
      public string name { get; set; }
      public string css_class { get; set; }
  }
  </code></pre>
  
  <p>I'm populating WSReturnUserGetById with the data on User, using this piece of code:</p>
  
  <pre><code>public T PopulateObjects&lt;T&gt;(object obj) where T : class, new()
  {
      if (obj == null) return null;
      T Obj = new T();
      PropertyInfo[] properties = obj.GetType().GetProperties();
      foreach (PropertyInfo p in properties)
      {
          PropertyInfo objPf = typeof(T).GetProperty(p.Name);
          if (objPf != null)
          {
              if (p.PropertyType == objPf.PropertyType)
              {
                  objPf.SetValue(Obj, p.GetValue(obj));
              }
          }
      }
      return Obj;
  }
  </code></pre>
  
  <p>I also have one function to make populate a list of objects</p>
  
  <p>public List PopulateObjectList(IEnumerable objects) where T: class, new()
      {</p>
  
  <pre><code>    List&lt;T&gt; response = new List&lt;T&gt;();
      foreach (U obj in objects)
      {
          T Obj = new T();
          if (obj != null)
          {
              PropertyInfo[] properties = obj.GetType().GetProperties();
              foreach (PropertyInfo p in properties)
              {
                  PropertyInfo objPf = typeof(T).GetProperty(p.Name);
                  if (objPf != null)
                  {
                      if (p.PropertyType == objPf.PropertyType)
                      {
                          objPf.SetValue(Obj, p.GetValue(obj));
                      }
                  }
              }
          }
          response.Add(Obj);
      }
  
      return response;
  }
  </code></pre>
  
  <p>When I use that code, it works except for the property "Interests" in User and WSReturnUserGetById, 'couse the type is different. So, i'm trying to adjust, and use this function to make it work:</p>
  
  <pre><code>public object populateCompleteObj&lt;T, U&gt;(U mainobj) where T : class, new()
  {
      if (mainobj.GetType().IsGenericType &amp;&amp; mainobj is IEnumerable)
      {
          List&lt;T&gt; response = new List&lt;T&gt;();
          foreach (object obj in (IEnumerable)mainobj)
          {
              T Obj = new T();
              if (obj != null)
              {
                  PropertyInfo[] properties = obj.GetType().GetProperties();
                  foreach (PropertyInfo p in properties)
                  {
                      PropertyInfo objPf = typeof(T).GetProperty(p.Name);
                      if (objPf != null)
                      {
                          if (typeof(IEnumerable).IsAssignableFrom(objPf.PropertyType))
                          {
                              objPf.SetValue(Obj, populateCompleteObj&lt;'objpf property class', 'obj property class'&gt;(p.GetValue(obj)));
                          }
                          else if (p.PropertyType == objPf.PropertyType)
                          {
                              objPf.SetValue(Obj, p.GetValue(obj));
                          }
                      }
                  }
              }
          }
  
      }
      else
      {
          if (mainobj == null) return null;
          T Obj = new T();
          PropertyInfo[] properties = mainobj.GetType().GetProperties();
          foreach (PropertyInfo p in properties)
          {
              PropertyInfo objPf = typeof(T).GetProperty(p.Name);
              if (objPf != null)
              {
                  if (p.PropertyType == objPf.PropertyType)
                  {
                      objPf.SetValue(Obj, p.GetValue(mainobj));
                  }
              }
          }
          return Obj;
      }
  
  }
  </code></pre>
  
  <p>The problem is that I don't know how to get the class of the property info, so a can do the recursion. Does anyone knows how to do this?</p>