【发布时间】: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