I have code that tries to create an object first with one constructor and then, if that fails, with the default constructor:
MyClass Construct(MyField f1, MyField f2)
{
try
{
return (MyClass)Activator.CreateInstance(typeof(MyClass), f1, f2);
}
catch
{
var o = (MyClass)Activator.CreateInstance(typeof(MyClass));
o.f1= f1;
o.f2=f2;
return o;
}
}
I want to prevent the debugger from stopping on the exception if it is caught. I tried [DebuggerStepThrough]
, [DebuggerHidden]
and [DebuggerNonUserCode]
without luck.
I also tried running: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VsRegEdit.exe" set "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community" HKLM Debugger\Engine AlwaysEnableExceptionCallbacksOutsideMyCode dword 1
as advised here but no luck.
Is there any way to do this in VS2017? Alternatively, is there a way to use Activator.CreateInstance
that will return null
instead of throwing an exception?
(using visual studio 2017 15.8.0 preview 4.0)