【问题标题】:How to get property type for array class inner properties如何获取数组类内部属性的属性类型
【发布时间】:2018-08-12 16:32:54
【问题描述】:

我试图获取数组属性的属性类型

[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; }

}

我需要获取基于 Orders.Customer.OtherAddress 或 Orders.Customer.0.OtherAddress 索引的类型。 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) });

我该如何继续?

【问题讨论】:

  • 某些原因你不能使用var a = dataSource[0].customer[0].OtherAddress;?
  • 嗨@JohnWu 我正在使用动态数据源。所以我需要独特的解决方案。
  • 我不明白你的意思。你是说dataSource 被输入为dynamic 吗?
  • 嗨@JohnWu dataSource 不是动态类型。数据将被动态分配

标签: c# propertyinfo


【解决方案1】:

OP,您的问题不是很清楚......“使用动态数据源”可能意味着很多不同的事情。但是,无论对数据源的引用是强类型对象、动态对象还是只是一个对象,被引用的对象仍然是某种强类型类。除非由于某种原因您无法设置对其类型库的引用,否则您不需要使用任何反射。最多一个简单的演员就可以做到。有了动态,这甚至都没有必要。

例如,如果你有这个接口:

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

...以及实现它的类....

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" } }
            }
        };
    }
}

您仍然可以访问其他地址而无需任何反射:

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();
    }
}

输出:

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

【讨论】:

  • 嗨,John,我想将此结构用于其他类定义。所以,我尝试了独特的解决方案。
【解决方案2】:

我假设您正在寻找可以应用于其他未知情况的“通用”答案。但是,我不确定您的情况会有多普遍/未知。我想出了这个获得“OtherAddress”类型的方法,但您仍然需要知道一些字段名称。如果您想要一个完全通用的方法,您将需要更广泛地提取各种属性。调试窗口中列出的类型是字符串。

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 2019-09-28
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多