У меня есть следующие классы:
Пользователь
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; }
}
Интерес
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; }
}
и WSReturnInterestBetById
public class WSReturnInterestGetById
{
public long idinterest { get; set; }
public string name { get; set; }
public string css_class { get; set; }
}
Я заполняю WSReturnUserGetById данными о пользователе, используя этот фрагмент кода:
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;
}
У меня также есть одна функция для заполнения списка объектов
общедоступный список PopulateObjectList (объекты IEnumerable), где 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;
}
Когда я использую этот код, он работает, за исключением свойства «Интересы» в User и WSReturnUserGetById, потому что тип другой. Итак, я пытаюсь настроить и использовать эту функцию, чтобы она работала:
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;
}
}
Проблема в том, что я не знаю, как получить информацию о классе свойства, поэтому я могу выполнить рекурсию. Кто-нибудь знает, как это сделать?