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. dataSource is the list of Orders.
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. 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. 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.