简体   繁体   English

仅在以反射方式创建对象时,在“释放”模式下例外

[英]Exception in Release mode only while reflectively creating objects

My application is failing only when I attempt to launch it in release mode. 仅当我尝试以发布模式启动它时,我的应用程序才会失败。 Debug mode works fine. 调试模式工作正常。 I am getting an error that reads 我收到一个读取的错误

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll PresentationFramework.dll中发生了类型为'System.Reflection.TargetInvocationException'的未处理异常

Additional information: Exception has been thrown by the target of an invocation. 附加信息:调用的目标已引发异常。

Because this only happens in release, it's been a bit difficult to debug. 因为这仅发生在发行版中,所以调试起来有点困难。 However, if I uncheck the "Optimize code" checkbox in the project properties, I am able to see an exception thrown in this method: 但是,如果取消选中项目属性中的“优化代码”复选框,则可以看到在此方法中引发的异常:

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

on this line: 在这条线上:

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

The exception reads: 异常显示为:

An exception of type 'System.NullReferenceException' occurred in LWDCloudManager.exe but was not handled in user code LWDCloudManager.exe中发生类型'System.NullReferenceException'的异常,但未在用户代码中处理

Additional information: Object reference not set to an instance of an object. 附加信息:对象引用未设置为对象的实例。

Does anyone have any suggestions on how I might be able either fix this issue, or how to dig deeper and understand why this could be happening in release mode only? 对于我如何能够解决此问题,或者如何更深入地了解为什么仅在发布模式下会发生这种情况,是否有人有任何建议?

I discovered the problem. 我发现了问题。 Actually kind of a stupid problem/oversight by me. 其实对我来说是一个愚蠢的问题/疏忽。 Recently my company decided that instead of throwing exceptions in internal methods, we would just use Debug.Asserts, so I went and changed all of them. 最近,我的公司决定不使用内部方法抛出异常,而是使用Debug.Asserts,因此我去更改了所有这些。 One of the lines of code ahead of this issue was a debug assert wrapped around a call to a TryGetValue call on a dictionary. 此问题之前的代码行之一是调试断言,该断言包裹在对字典的TryGetValue调用的调用周围。 Because I was attempting to run in release mode, the debug.assert was not executed, thus the value was not retrieved from the dictionary resulting in a null being passed. 因为我试图在发布模式下运行,所以未执行debug.assert,因此未从字典中检索值,导致传递了null。 Thanks for the help guys 谢谢你们的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM