Search code examples
c#.net.net-3.5.net-4.0version-compatibility

GetProperty works in .NET 3.5 but not in .NET 4


I use following code:

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

It works in .NET 3.5. But if I change to .NET 4, then mi becomes null. Why?


Solution

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