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

时间:2017-11-10 05:12:32

标签: c# asp.net linq reflection

如何获取数组子属性的propertyinfo。如何在customer[0]

中访问OtherAddress属性
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));

访问表格操作

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;

2 个答案:

答案 0 :(得分:2)

假设您有以下类:

const searchText = 'Di';
const countrySearch = "USA";

const myArray = [{
      name: "Tom",
      country: "UK",
      active: true
    },
    {
      name: "Dick",
      country: "USA",
      active: false
    },
    {
      name: "Dimmon",
      country: "FR",
      active: false
    }
  ]
  .filter(({active, name, country}) =>
    active 
    || countrySearch && countrySearch === country
      && searchText && name.includes(searchText)
    || searchText && !countrySearch && name.includes(searchText)
    || !searchText && countrySearch && countrySearch === country   
  );
  
console.log(myArray);

然后您可以获取top.window.moveTo(0,0); if (document.all) { top.window.resizeTo(screen.availWidth,screen.availHeight); } else if (document.layers||document.getElementById) { if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){ top.window.outerHeight = screen.availHeight; top.window.outerWidth = screen.availWidth; } } 属性的document.elementFromPoint(92, 309).click();,如下所示:

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

重点是如何从Type Customer.OtherAddress形成属性var propInfo = typeof(Orders) .GetProperty("CustomersArray") // Orders -> Orders.CustomersArray .PropertyType.GetElementType() // Customer[] -> Customer .GetProperty("OtherAddress"); // Customer -> Customer.OtherAddress 。由于Type的{​​{1}}是数组,因此您必须使用Customer来获取数组项的Orders.CustomersArray,然后继续沿着路径前进。

在给定字符串路径的情况下,可以检索Type Orders.CustomersArray的通用代码如下所示:

GetElementType()

另请注意,我在Type循环中将Type替换为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; 来自Linq并且它是O(n)操作 - - 它在.Count()循环的每次迭代中对.Length数组执行循环,这使得for循环为O(N ^ 2)。但这只是一个侧面说明。

答案 1 :(得分:1)

这是一种简单的方法 -

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

这只是一个例子。 GetProperty(<name>)接受区分大小写的属性名称。

在你的情况下,你必须做这样的事情 -

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

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

complexData[2]OtherAddress的媒体资源名称。您可以遍历complexData以检索其他属性的属性信息,但请确保以区分大小写的方式正确传递属性名称。