GetProperty适用于.NET 3.5,但不适用于.NET 4

时间:2010-08-26 09:59:15

标签: c# .net .net-3.5 .net-4.0 version-compatibility

我使用以下代码:

protected object GetProperty(object target, string fieldName)
{
    Type type = target.GetType();
    PropertyInfo mi = type.GetProperty(
        fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
    object obj = mi.GetValue(target, null);
    return obj;
}

它适用于.NET 3.5。但如果我改为.NET 4,则mi变为null。为什么呢?

1 个答案:

答案 0 :(得分:5)

目标是否仍具有.net 4中的所需属性?有很多API更改。