简体   繁体   English

如何获取数组类内部属性的属性类型

[英]How to get property type for array class inner properties

I have tried to get a property type for array properties我试图获取数组属性的属性类型

[Serializable]
public class Orders
{
    public long OrderID { get; set; }
    public string CustomerID { get; set; }
    public int EmployeeID { get; set; }
    public double Freight { get; set; }
    public string ShipCountry { get; set; }
    public string ShipCity { get; set; }
    public Customer[] customer {get; set;}
}

public class Customer
{
    public string OtherAddress { get; set; }
    public int CustNum { get; set; }

}

I need to get type of Orders.Customer.OtherAddress or Orders.Customer.0.OtherAddress index based.我需要获取基于Orders.Customer.OtherAddressOrders.Customer.0.OtherAddress索引的类型。 dataSource is the list of Orders. dataSource是订单列表。

Type type = dataSource.GetElementType();

PropertyInfo propInfo = type.GetProperty("Customer");

DefaultMemberAttribute defaultMember = 
(DefaultMemberAttribute)Attribute.GetCustomAttribute(propInfo.PropertyType, 
typeof(DefaultMemberAttribute));
                    propInfo = 
propInfo.PropertyType.GetProperty("CustNum" , new Type[] { 
typeof(int) });

How can i proceed ?我该如何继续?

OP, your question isn't very clear.... "using dynamic datasource" could mean a lot of different things. OP,你的问题不是很清楚......“使用动态数据源”可能意味着很多不同的东西。 However, whether the reference to your data source is a strongly typed object, a dynamic, or just an object, the object being referenced is still a strongly-typed class of some kind.但是,无论对数据源的引用是强类型对象、动态对象还是只是对象,被引用的对象仍然是某种强类型类。 Unless for some reason you can't set a reference to its type library, you should not need to use any reflection.除非由于某种原因您不能设置对其类型库的引用,否则您不需要使用任何反射。 At most a simple cast will do it.最多一个简单的演员就可以做到。 With a dynamic, that isn't even necessary.对于动态,这甚至没有必要。

For example, if you have this interface:例如,如果您有这个界面:

public interface IRepository
{
    dynamic GetListAsDynamic();
    object  GetListAsObject();
}

...and a class that implements it.... ...和一个实现它的类....

public class OrderRepository : IRepository
{
    public dynamic GetListAsDynamic()
    {
        return new List<Order>
        {
            new Order
            {
                customer = new [] {new Customer { OtherAddress = "1234 Foo St" } }
            }
        };
    }
    public object GetListAsObject()
    {
        return new List<Order>
        {
            new Order
            {
                customer = new [] {new Customer { OtherAddress = "1234 Bar St" } }
            }
        };
    }
}

You can still access the other address without any reflection:您仍然可以访问另一个地址而无需任何反射:

public class Program
{
    public static void TestDynamic()
    {
        var repo = new OrderRepository();
        var orders = repo.GetListAsDynamic();
        var order = orders[0];
        var customers = order.customer;
        var customer = customers[0];

        Console.WriteLine("And the customer dynamic's other address is.... '{0}'", customer.OtherAddress);
    }

    public static void TestObject()
    {
        var repo = new OrderRepository();
        var orders = repo.GetListAsObject() as List<Order>;
        var order = orders[0];
        var customers = order.customer;
        var customer = customers[0];

        Console.WriteLine("And the customer object's other address is.... '{0}'", customer.OtherAddress);
    }

    public static void Main()
    {
        TestDynamic();
        TestObject();
    }
}

Output:输出:

And the customer dynamic's other address is.... '1234 Foo St'
And the customer object's other address is.... '1234 Bar St'

Working code on DotNetFiddle DotNetFiddle 上的工作代码

I assume that you are looking for a "generic" answer that could be applied to other, unknown, situations.我假设您正在寻找可以应用于其他未知情况的“通用”答案。 However, I am not sure how generic / unknown your situation will be.但是,我不确定您的情况会有多普遍/未知。 I came up with this which gets the the type of "OtherAddress" but you will still need to know some of the field names.我想出了这个获取“OtherAddress”类型的方法,但您仍然需要知道一些字段名称。 If you want a totally generic approach, you will need a more extensive extraction of the various properties.如果您想要一个完全通用的方法,您将需要对各种属性进行更广泛的提取。 The type listed in the debug window is string.调试窗口中列出的类型是字符串。

public void GetTypeInfo()
{
    Orders o = new Orders {customer = new Customer[] {new Customer()}};

    dataSource.Add(o);

    PropertyInfo info1 = dataSource[0].GetType().GetProperty("customer");

    Array c = (Array) info1.GetValue(dataSource[0], null);

    Type info2 = c.GetValue(0).GetType();

    PropertyInfo info3 = info2.GetProperty("OtherAddress");

    Debug.WriteLine("type| " + info3.PropertyType.Name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取属性基于某种类型的类的所有属性 - How to get all properties of a class where the property is based on a certain type 如何在 class 属性内设置 object 数组 - How to set a array of object inner a class property 如何获取数组中类的属性 - How to get properties of a class in an array 如果一个类有另一个类的属性,如何判断内部类的属性是否匹配 - If a class has a property of another class, how to tell if the properties of the inner class match 如何创建具有 1 个已知属性的泛型方法。 想要使用 Generic Class 属性动态获取不同类中的属性 - How to Create a generic method with 1 known property. Want to dynamic get the properties in different class with Generic Class property 如何通过反射获取类中属性的“类类型”? - how to get the “class type” of a property in a class through reflection? 如何从类中获取特定类型的所有可枚举属性 - How to get all enumerable properties of a specific type from a class 如何使用泛型类型参数获取类的所有属性 - How to get all properties of a class with generic type parameter 如何在没有反射的情况下获取类中指定类型的所有属性 - How to get all the properties of a specified type in a class without reflection c# 反射,如何获得泛型类型 class 属性 - c# Reflection, how to get generic type class properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM