仅在反射创建对象时在 Release 模式下出现异常

Exception in Release mode only while reflectively creating objects

我的应用程序只有在我尝试以发布模式启动时才会失败。调试模式工作正常。我收到一条错误消息

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

Additional information: Exception has been thrown by the target of an invocation.

因为这只发生在发布中,调试起来有点困难。但是,如果我取消选中项目属性中的“优化代码”复选框,我会看到此方法中抛出的异常:

 private static T MakeObject<T>(Type type) where T :class
    {
        //Default reflective behavior to create an instance with an empty constructor
        //
        //*note: .GetConstructor can return null.
        object obj = null;
        T tObj = default(T);
        
        ConstructorInfo ci = type.GetConstructor(TypeInfo.EmptyTypes);

        if (ci != null)
        {
            obj = ci.Invoke(new object[] { /* Empty */});

            tObj = obj as T;
        }

        if (tObj == null)
            throw new InvalidCastException("Fatal error occurred within NavigationService (GetConstructor). Type: " + type.ToString());

        return tObj;
    }

这一行:

ConstructorInfo ci = type.GetConstructor(TypeInfo.EmptyTypes);

异常显示为:

An exception of type 'System.NullReferenceException' occurred in LWDCloudManager.exe but was not handled in user code

Additional information: Object reference not set to an instance of an object.

有没有人对我如何解决这个问题,或者如何更深入地挖掘并理解为什么这可能只在发布模式下发生有任何建议?

我发现了问题。实际上我有点愚蠢problem/oversight。最近我的公司决定不在内部方法中抛出异常,我们只使用 Debug.Asserts,所以我去更改了所有这些方法。此问题之前的代码行之一是围绕对字典的 TryGetValue 调用的调用包裹的调试断言。因为我试图在发布模式下 运行,所以 debug.assert 没有被执行,因此没有从字典中检索到值,导致传递了一个空值。感谢大家的帮助