I have the following classes:
User
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<Interest> Interests { get; set; }
}
Interest
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<User> Users { get; set; }
}
WSReturnUserGetById
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<WSReturnInterestGetById> Interests { get; set; }
}
and WSReturnInterestBetById
public class WSReturnInterestGetById
{
public long idinterest { get; set; }
public string name { get; set; }
public string css_class { get; set; }
}
I'm populating WSReturnUserGetById with the data on User, using this piece of code:
public T PopulateObjects<T>(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;
}
I also have one function to make populate a list of objects
public List PopulateObjectList(IEnumerable objects) where T: class, new() {
List<T> response = new List<T>();
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;
}
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:
public object populateCompleteObj<T, U>(U mainobj) where T : class, new()
{
if (mainobj.GetType().IsGenericType && mainobj is IEnumerable)
{
List<T> response = new List<T>();
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<'objpf property class', 'obj property class'>(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;
}
}
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?