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

mag*_*gol 1 .net c# .net-4.0 .net-3.5 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;
}
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*tum 5

Does the target still have the desired property in .net 4? There were quite a few API Changes.

  • @Magol:这是依赖私人和/或无证件的风险 - 他们可以而且确实会改变. (4认同)