【发布时间】:2009-05-13 09:14:13
【问题描述】:
我在获取嵌套对象属性时遇到问题。对于我正在使用的示例,我有 2 个类:
public class user
{
public int _user_id {get; set;}
public string name {get; set;}
public category {get; set;}
}
public class category
{
public int category_id {get; set;}
public string name {get; set;}
}
那里很简单,如果我反映其中任何一个,我就会得到正确的 GetProperties() 集合,例如,如果我这样做:
PropertyInfo[] props = new user().GetType().GetProperties();
我将获得属性 user_id、name 和 category,如果我这样做:
PropertyInfo[] props = new category().GetType().GetProperties();
我会得到属性category_id和category;这很好用。 但是,这就是我感到困惑的地方......
如您所见,category 是 user 的最后一个属性,如果我这样做的话
//this gets me the Type 'category'
Type type = new user().GetType().GetProperties().Last().PropertyType;
//in the debugger, I get "type {Name='category', FullName='category'}"
//so I assume this is the proper type, but when I run this:
PropertyInfo[] props = type.GetType().GetProperties();
//I get a huge collection of 57 properties
知道我在哪里搞砸了吗?这个可以吗?
【问题讨论】:
标签: c# reflection