简体   繁体   English

如何获取数组嵌套属性的propertyinfo

[英]How to get propertyinfo of array nested property

How can i get the propertyinfo of array child property. 我如何获取数组子属性的propertyinfo。 How can i access OtherAddress property in customer[0] 如何访问customer[0] OtherAddress属性customer[0]

List<Orders> order = new List<Orders>(); 

Customer[] cs = { new Customer { CustNum = 5, OtherAddress = "Hello" }, new Customer { CustNum = 986, OtherAddress = "Other" } };

Customer[] cso = { new Customer { OtherAddress = "T", CustNum = 5 }, new Customer { CustNum = 777, OtherAddress = "other" } };

order.Add(new Orders(code + 1, "ALFKI", i + 0, 2.3 * i, "Mumbari", "Berlin", cs));
order.Add(new Orders(code + 2, "ANATR", i + 2, 3.3 * i, "Sydney", "Madrid", cso));
order.Add(new Orders(code + 3, "ANTON", i + 1, 4.3 * i, "NY", "Cholchester", cs));
order.Add(new Orders(code + 4, "BLONP", i + 3, 5.3 * i, "LA", "Marseille", cso));
order.Add(new Orders(code + 5, "BOLID", i + 4, 6.3 * i, "Cochin", "Tsawassen", cs));

To access the table operation 访问表操作

complexData = "customer.0.OtherAddress".split('.'); 
type = typeof(orders);

PropertyInfo propInfo = type.GetProperty(complexData[0]);
for (var i = 1; i < complexData.Count(); i++)
{
    propInfo = propInfo.PropertyType.GetProperty(complexData[i]);
}
return propInfo.PropertyType;

Assuming you have classes like follows: 假设您有如下类:

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

public class Orders
{
    public Orders(/* ..other parameters.. */ Customer[] customers)
    {
        this.CustomersArray = customers;
    }

    // I use name CustomersArray instead of Customer to make the answer
    // easier to read
    public Customer[] CustomersArray { get; set; }
}

Then you can get the Type of the Customer.OtherAddress property as follows: 然后,您可以获取Customer.OtherAddress属性的Type ,如下所示:

var propInfo = typeof(Orders)
   .GetProperty("CustomersArray")  // Orders -> Orders.CustomersArray
   .PropertyType.GetElementType()  // Customer[] -> Customer
   .GetProperty("OtherAddress");   // Customer -> Customer.OtherAddress

The main point is how to get the Type of Customer form the property Orders.CustomersArray . 要点是如何从Orders.CustomersArray属性获取“ Customer Type ”。 Since the Type of Orders.CustomersArray is array, you have to use GetElementType() to get the Type of array item, then proceed further down the path. 由于Orders.CustomersArrayType是数组,因此必须使用GetElementType()来获取数组项的Type ,然后再沿路径继续进行。

The generalized code that can retrieve Type of Customer.OtherAddress given a string path looks like this: 给定字符串路径,可以检索Type of Customer.OtherAddress的通用代码如下:

var complexData = "CustomersArray.0.OtherAddress".Split('.');
var type = typeof(Orders);
PropertyInfo propInfo = null;

for (var i = 0 ; i < complexData.Length ; i++)
{
    if (complexData[i] == "0")
    {
        type = type.GetElementType();
    }
    else
    {
        propInfo = type.GetProperty(complexData[i]);
        type = propInfo.PropertyType;
    }
}

return propInfo.PropertyType;

Also note that I replaced .Count() with .Length in the for loop, because .Count() comes from Linq and it's an O(n) operation -- it performs a loop over the complexData array every iteration of the for loop, which makes your for loop O(N^2). 另外请注意,我换成.Count().Lengthfor循环,因为.Count()来自LINQ和它是一个为O(n)操作-它执行了一个循环complexData阵列的每一次迭代for循环,这使您的for循环为O(N ^ 2)。 But this is just a side note. 但这只是一个旁注。

Here's a simple way you can do this- 这是您执行此操作的一种简单方法-

class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

List<Customer> ls = new List<Customer>();
ls.Add(new Customer() { ID = 1, Name = "Name 1" });
ls.Add(new Customer() { ID = 2, Name = "Name 2" });

PropertyInfo info = ls[0].GetType().GetProperty("ID");

This is just an illustration. 这只是一个例子。 GetProperty(<name>) accepts property name in case sensitive format. GetProperty(<name>)接受区分大小写格式的属性名称。

In your case you have to do something like this- 在您的情况下,您必须执行以下操作-

complexData = "Customer.0.OtherAddress".split('.'); 

PropertyInfo propInfo = cs[0].GetType().GetProperty(complexData[2]); //access cs through index

complexData[2] is the property name for OtherAddress . complexData[2]OtherAddress的属性名称。 You can loop through the complexData to retrieve property info of other properties, but make sure you pass the property name correctly in case sensitive way. 您可以遍历complexData来检索其他属性的属性信息,但请确保以区分大小写的方式正确传递属性名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM