如何获取“班级类型”通过反射来获取类中的属性?

发布于 2024-11-02 05:19:02 字数 945 浏览 2 评论 0原文

的一段代码

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-11-09 05:19:02

您可以使用 GetGenericArguments() 方法并执行以下操作:

typeof(Order).GetProperty("OrderDetails").PropertyType.GetGenericArguments()[0]

You can use the GetGenericArguments() method and do:

typeof(Order).GetProperty("OrderDetails").PropertyType.GetGenericArguments()[0]
苍白女子 2024-11-09 05:19:02

PropertyInfo.PropertyType 返回的类型将是 typeof(List)

使用此您可以使用 Type.GetGenericArguments() 获取类型参数

Type propertyType = property.PropertyType;

if(propertyType.IsGeneric && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
    Type argType = propertyType.GetGenericArguments()[0];
    // argType will give you your OrderDetail
}

The type returned by PropertyInfo.PropertyType would be typeof(List<OrderDetail>)

using this you can get the type arguments using Type.GetGenericArguments()

Type propertyType = property.PropertyType;

if(propertyType.IsGeneric && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
    Type argType = propertyType.GetGenericArguments()[0];
    // argType will give you your OrderDetail
}
停滞 2024-11-09 05:19:02

您是否正在寻找如何了解通用参数?

假设您在某个变量(例如“someOrder”)中有一个 Order 实例:

someOrder.OrderDetails.GetType().GetGenericArguments();

由于 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":

someOrder.OrderDetails.GetType().GetGenericArguments();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文