[英]How to get propertyinfo of array nested property
我如何获取数组子属性的propertyinfo。 如何访问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));
访问表操作
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;
假设您有如下类:
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; }
}
然后,您可以获取Customer.OtherAddress
属性的Type
,如下所示:
var propInfo = typeof(Orders)
.GetProperty("CustomersArray") // Orders -> Orders.CustomersArray
.PropertyType.GetElementType() // Customer[] -> Customer
.GetProperty("OtherAddress"); // Customer -> Customer.OtherAddress
要点是如何从Orders.CustomersArray
属性获取“ Customer
Type
”。 由于Orders.CustomersArray
的Type
是数组,因此必须使用GetElementType()
来获取数组项的Type
,然后再沿路径继续进行。
给定字符串路径,可以检索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;
另外请注意,我换成.Count()
用.Length
在for
循环,因为.Count()
来自LINQ和它是一个为O(n)操作-它执行了一个循环complexData
阵列的每一次迭代for
循环,这使您的for
循环为O(N ^ 2)。 但这只是一个旁注。
这是您执行此操作的一种简单方法-
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
来检索其他属性的属性信息,但请确保以区分大小写的方式正确传递属性名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.