现在又俩个实体:
public class User{
public string Id { get; set; }
public string Email { get; set; }
public University University { get; set; }
}
public class University{
public string Id { get; set; }
public stUring Name{ get; set; }
}
程序入口
public void Main(){
User u=new User();
//赋值
string str= Serialize(u);
}
public string Serialize( object o)
{
//在这里这样获取University的Name属性值。
}
当然以上并不符合实际开发环境。只是想知道递归反射取值。
大侠们帮忙一下????
Type type = o.GetType();
PorpertyInfo pUniversity = type.GetProperty("University");
Type tUniversity = pUniversity.ReturnType;
PropertyInfo pName = tUniversity.GetProperty("Name");
string name = pName.GetValue(pUniversity.GetValue(o, null), null);
return name;
PropertyInfo[] ps = o.GetType().GetProperties();
foreach (PropertyInfo p in ps)
{
if (p.CanRead)
p.GetValue(o, null);
}