[英]How to get property type for array class inner properties
I have tried to get a property type for array properties我试图获取数组属性的属性类型
[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; }
}
I need to get type of Orders.Customer.OtherAddress or Orders.Customer.0.OtherAddress index based.我需要获取基于Orders.Customer.OtherAddress或Orders.Customer.0.OtherAddress索引的类型。 dataSource is the list of Orders.
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) });
How can i proceed ?我该如何继续?
OP, your question isn't very clear.... "using dynamic datasource" could mean a lot of different things. OP,你的问题不是很清楚......“使用动态数据源”可能意味着很多不同的东西。 However, whether the reference to your data source is a strongly typed object, a dynamic, or just an object, the object being referenced is still a strongly-typed class of some kind.
但是,无论对数据源的引用是强类型对象、动态对象还是只是对象,被引用的对象仍然是某种强类型类。 Unless for some reason you can't set a reference to its type library, you should not need to use any reflection.
除非由于某种原因您不能设置对其类型库的引用,否则您不需要使用任何反射。 At most a simple cast will do it.
最多一个简单的演员就可以做到。 With a dynamic, that isn't even necessary.
对于动态,这甚至没有必要。
For example, if you have this interface:例如,如果您有这个界面:
public interface IRepository
{
dynamic GetListAsDynamic();
object GetListAsObject();
}
...and a class that implements it.... ...和一个实现它的类....
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" } }
}
};
}
}
You can still access the other address without any reflection:您仍然可以访问另一个地址而无需任何反射:
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();
}
}
Output:输出:
And the customer dynamic's other address is.... '1234 Foo St'
And the customer object's other address is.... '1234 Bar St'
I assume that you are looking for a "generic" answer that could be applied to other, unknown, situations.我假设您正在寻找可以应用于其他未知情况的“通用”答案。 However, I am not sure how generic / unknown your situation will be.
但是,我不确定您的情况会有多普遍/未知。 I came up with this which gets the the type of "OtherAddress" but you will still need to know some of the field names.
我想出了这个获取“OtherAddress”类型的方法,但您仍然需要知道一些字段名称。 If you want a totally generic approach, you will need a more extensive extraction of the various properties.
如果您想要一个完全通用的方法,您将需要对各种属性进行更广泛的提取。 The type listed in the debug window is string.
调试窗口中列出的类型是字符串。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.