在MATLAB编译器生成的c#中初始化dll时出现exception

我使用MATLAB编译器生成一个带有很少MATLAB代码的.NET Assembly

 function output_arg = extest( input_arg1,input_arg2 ) output_arg = input_arg1+input_arg2; end 

我用向导生成了dll。

在我的Visual Studio项目中,我添加了对生成的dll( extest.dll )和MATLAB Runtime dll的引用( C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll ),如“程序集说明”中所述。

这是我的c#代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MathWorks.MATLAB.NET.Utility; using extest; namespace DllTesting { class Program { static void Main(string[] args) { ExClass e1 = new ExClass(); } } } 

它构建没有错误,知识分子正在工作(所以所有引用应该是我的理解中的好)

但是当我启动它时,抛出以下exception(在new ExClass() ):

DllTesting.exe中出现未处理的“System.TypeInitializationException”类型exception

附加信息:’extest.ExClass’的类型初始值设定项引发了exception。

有什么建议这个代码有什么问题或丢失了什么?

尝试在类定义之前添加它

 [assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")] 

还要确保用于汇编的.NET版本与用于Visual Studio项目的版本相同或更低。

另一种解决方案可能是将MATLAB运行时的路径(例如C:\ Program Files \ MATLAB \ MATLAB Runtime \ v92 \ runtime \ win64)添加到PATH环境变量。

如果这些都没有帮助,看看这里和这里 ,你可能有64/32位不匹配。