We are developing an existing application currently targeting .NET Framework 3.5. In the new version, the application should run on .NET 4.5. For testing, I upgraded the projects to use .NET Framework 4.5.
First it seemed that everything is working correctly. The application (WinForm) ran as expected. Nothing happens when I start the executable directly and everything seems to work correctly. But when I start the application in debug from Visual Studio, many NullReferenceExceptions are thrown, on code where it does not with .NET Framework 3.5.
The affected snippet is as follows:
public string ConsistencyContextKey
{
get { return Container.ConsistencyCheckRuleCo.ContextKey; }
}
The Property ConsistencyCheckRuleCo
is null in the exception case.
The call to the property is via reflection. In .NET 3.5, the call comes from System.ComponentModel.ReflectPropertyDescriptor.GetValue(object)
.
See CallStack:
In 4.5 this method was changed and now pass on to the method [System.SecurityUtils.MethodInfoInvoke(MethodInfo, object, object[])]
.
See CallStack:
In the end of both the property gets called via method.invoke()
.
The source for SecurityUtils.MethodInfoInvoke
can be found here and of ReflectPropertyDescriptor.GetValue()
here.
I know it's the source from 4.6.1, but nothing has changed and in 4.5 it's the same.
The mysterious thing is that in the ReflectPropertyDescriptor
, there is an Try-Catch also in 3.5 and 4.5. So why does Visual Studio say there is a user-unhandled exception when the code comes to my property?
The Property is public, not generic and the type is visible.