0

我的 c# 程序使用 Matlab dll,只要我在调试模式下运行它就可以找到它。但是当我尝试在发布模式下运行它时,它会在创建它的实例时立即崩溃。

它引发 TypeInitializationException。

谢谢你的帮助

堆栈跟踪:

"at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
 at System.Environment.get_StackTrace()
 at Turbo_Neuron.ANNController..ctor() 
    in C:\\Users\\Eli\\Desktop\\....\\MyProject\\Program.cs:line 17
        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
        at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
        at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
        at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
        at System.Activator.CreateInstance(ActivationContext activationContext)
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
        at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        at System.Threading.ThreadHelper.ThreadStart()"

它说:

The type initializer for 'MatlabANNComp.MatlabANN' threw an exception.

StachTrace 2:

   at MatlabANNComp.MatlabANN..ctor()
   at Turbo_Neuron.ANNController..ctor() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Matlab_Layer\ANNController.cs:line 49
   at Turbo_Neuron.TNController..ctor(pnl_main form) in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Logic_Layer\TNController.cs:line 49
   at Turbo_Neuron.pnl_main..ctor() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\View_Layer\Turbo_Form.cs:line 29
   at Turbo_Neuron.Program.Main() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at System.Activator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

异常消息:

Exception.Message = 
"The type initializer for 'MatlabANNComp.MatlabANN' threw an exception."

InnerException.Message =
{"Could not load file or assembly 'MWArray, Version=2.10.1.0, 
Culture=neutral, PublicKeyToken=e1d84a0da19db86f' 
or one of its dependencies. An attempt was made 
to load a program with an incorrect format."}

InnerException.InnerException = null
4

1 回答 1

0

An attempt was made to load a program with an incorrect format

这是关键,这意味着您正在混合平台 somwhow。32 位进程无法加载 64 位 dll,反之亦然。如果一个进程无论如何都试图这样做,它会抛出上述异常。你说你只是从 Debug->Release 改变的,所以它很可能与平台改变有关。比较配置管理器中所有项目的设置。它们都应该是 x86 或 x64(不是AnyCPU)。哪一个取决于您的 matlab dll 的平台(在构建时设置)。

于 2013-09-10T16:56:12.603 回答