3

我的问题是:如何使

PropertyInfo.GetValue(object, null);

返回值转换为 PropertyType,而不是返回对象。

我试过 Convert.ChangeType 但不是运气。

谢谢你。

更新1:

更多详情:

我的代码:

foreach (var propertyInfo in customerInfo.CustomerRelationship.GetType().GetProperties())
{
    var relationshipList = (IList)propertyInfo.GetValue(customerInfo.CustomerRelationship, null);
    foreach (var relationship in relationshipList)
    {
    }
}

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; }
    ..........
}

因为我不能动态转换每个我无法比较的关系项目显示,所以查询(Linq)使用数据库进行操作。

4

1 回答 1

3

你不能。

为简单起见,可以在其上编写通用包装器。

public static T GetValue<T>(Object obj1, Object obj2)
{
return (T)PropertyInfo.GetValue(obj1, obj2);
}
于 2012-10-15T14:41:19.407 回答