1

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:

enter image description here

In 4.5 this method was changed and now pass on to the method [System.SecurityUtils.MethodInfoInvoke(MethodInfo, object, object[])]. See CallStack:

enter image description here

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Mark Kowalski
  • 264
  • 2
  • 9
  • Reflection was changed heavily in 4.5 to support WinRT, that does not have anything to do with this. Just ensure that you did not ask the debugger to stop at any thrown exception. Debug > Exceptions dialog. – Hans Passant Mar 22 '16 at 15:46
  • I dont understand why Visual Sudio says user-unhandeld Exception. In the ReflectPropertyDescriptor.GetValue() which is the base of the Call there is a Try-Catch-Block. – Mark Kowalski Mar 22 '16 at 15:53

1 Answers1

0

The Reason why Visual Studio says user-unhandled Exception is because of the setting "Enable just my code". The debugger cant know that the method ReflectPropertyDescriptor.GetValue() from the .NET Framework has an Try-Catch-Block. If you disable this setting the warning doesnt show anymore. I got the hint from this stackoverflow question.

Another observation that I have made is that if in 3.5 a exception occurs the Exception would be forwarded. In 4.5 the exception would be thrown directly on the position where it happens. This only applies if the calling of the property is via reflection and the method MethodInfo.Invoke().

I wrote a test application and on 3.5 the exception has been forwarded to the Method where i call MethodInfo.Invoke(). In 4.5 i got the message "user-unhandled exception". That means something has been changed in handling Exception from reflection-calls.

Community
  • 1
  • 1
Mark Kowalski
  • 264
  • 2
  • 9