Reputation: 31
My question is: How can make
PropertyInfo.GetValue(object, null);
return value with casted to PropertyType, not return object.
I tried Convert.ChangeType but not luck.
Thank you.
Update 1:
More detail:
My code:
foreach (var propertyInfo in customerInfo.CustomerRelationship.GetType().GetProperties())
{
var relationshipList = (IList)propertyInfo.GetValue(customerInfo.CustomerRelationship, null);
foreach (var relationship in relationshipList)
{
}
}
with
public struct CustomerInfo
{
public Customer CustomerItem { get; set; }
public Relationship CustomerRelationship { get; set; }
}
public class Relationship
{
public List<Contact> ContactItems { get; set; }
public List<Department> DepartmentItems { get; set; }
..........
}
Because i can't dynamic cast each Relationship Item show i can't compare, query (Linq) manipulate with database .
Upvotes: 3
Views: 2988
Reputation: 30728
You can't.
For the sake of simplicity, a generic wrapper can be written over it.
public static T GetValue<T>(Object obj1, Object obj2)
{
return (T)PropertyInfo.GetValue(obj1, obj2);
}
Upvotes: 3