我有以下课程:
用户
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;
}
我还有一个功能可以填充对象列表
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;
}
当我使用该代码时,除了 User 和 WSReturnUserGetById 中的属性“Interests”之外,它都可以工作,因为类型不同。所以,我正在尝试调整,并使用此功能使其工作:
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;
}
}
问题是我不知道如何获取属性信息的类,所以可以进行递归。有谁知道该怎么做?