如何获取“班级类型”通过反射来获取类中的属性?
的一段代码
public class Order
{
//Primary Key
public long OrderId { get; set; }
//Many to One Relationship
[ParentObject("PersonId")]
public Person Buyer { get; set; }
//One to Many Relationship
[ChildObject("OrderId")]
public List<OrderDetail> OrderDetails { get; set; }
public decimal TotalAmount
{
get
{
if (OrderDetails == null)
return 0;
return OrderDetails.Sum(o => o.LineItemCost);
}
}
public string Notes { get; set; }
}
这是我试图检查 Order 对象 。现在我想做的是:获取所有属性并尝试找出类类型,例如
public List<OrderDetail> OrderDetails { get; set; }
我想获取另一个类的“OrderDetail”类型。当我尝试使用 PropertyInfo.PropertyType 获取它时,我得到“List1”(通用类型),PropertyInfo.GetType() 给出一些 System.Reflection.RuntimeType,PropertyInfo.DeclaringType 给出“Order”(包含该属性的类)。如果有人能提出解决方案,我将不胜感激。提前致谢。
here is a piece of code
public class Order
{
//Primary Key
public long OrderId { get; set; }
//Many to One Relationship
[ParentObject("PersonId")]
public Person Buyer { get; set; }
//One to Many Relationship
[ChildObject("OrderId")]
public List<OrderDetail> OrderDetails { get; set; }
public decimal TotalAmount
{
get
{
if (OrderDetails == null)
return 0;
return OrderDetails.Sum(o => o.LineItemCost);
}
}
public string Notes { get; set; }
}
I am trying to examine the Order object. Now what I want to do is that: get all the properties and try to find out the class type for instance
public List<OrderDetail> OrderDetails { get; set; }
I want to get the type "OrderDetail" that is another class. When I try to get it using PropertyInfo.PropertyType I get "List1" (the Generic type), PropertyInfo.GetType() gives some System.Reflection.RuntimeType, PropertyInfo.DeclaringType gives "Order" (the class that contains the property). I would be obliged if anyone can propose a solution. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 GetGenericArguments() 方法并执行以下操作:
You can use the GetGenericArguments() method and do:
PropertyInfo.PropertyType 返回的类型将是
typeof(List)
使用此您可以使用
Type.GetGenericArguments()
获取类型参数The type returned by PropertyInfo.PropertyType would be
typeof(List<OrderDetail>)
using this you can get the type arguments using
Type.GetGenericArguments()
您是否正在寻找如何了解通用参数?
假设您在某个变量(例如“someOrder”)中有一个 Order 实例:
由于 List 有一个泛型参数,因此 Type.GetGenericArguments 方法将返回一个项目数组:Order 类型。
它正在解决你的问题,不是吗?
看来您不知道 OrderDetails 类型是 List`1 (具有单个泛型参数的泛型列表)。 PropertyInfo.PropertyType 公开了这一点,因为此类属性的类型是通用列表。您想知道该通用列表的通用类型。
Are you looking for how to know about generic arguments?
Let's say you've an instance of Order in some variable like "someOrder":
Since List has a single generic parameter, Type.GetGenericArguments method will return an array of an item: Order type.
It's solving your problem, isn't it?
It seems you didn't know that OrderDetails type is List`1 (a generic list with a single generic parameter). PropertyInfo.PropertyType exposes that because the type of such property is a generic list. You want to know the generic type of that generic list.