我试图在 C# 类库 中使用 .dll,在 MATLAB 和 Matlab .net Complier 中构建,但程序每次初始化 MWArray.dll 中的对象时都会引发异常,例如:
MWNumericArray m = new MWNumericArra(10,10);
异常(exception):
System.TypeInitializationException was caught
HResult=-2146233036
Message=The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' threw an exception.
Source=MWArray
TypeName=MathWorks.MATLAB.NET.Arrays.MWNumericArray
StackTrace:
at MathWorks.MATLAB.NET.Arrays.MWNumericArray.op_Implicit(Double[] values)
at VolCalc.Vol.CalculateVolatility(Double[] data)
InnerException: System.TypeInitializationException
HResult=-2146233036
Message=The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWIndexArray' threw an exception.
Source=MWArray
TypeName=MathWorks.MATLAB.NET.Arrays.MWIndexArray
StackTrace:
at MathWorks.MATLAB.NET.Arrays.MWIndexArray..ctor()
at MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(Double scalar)
at MathWorks.MATLAB.NET.Arrays.MWNumericArray.get__Inf()
at MathWorks.MATLAB.NET.Arrays.MWNumericArray..cctor()
InnerException: System.Security.SecurityException
HResult=-2146233078
Message=Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Source=MWArray
StackTrace:
at MathWorks.MATLAB.NET.Utility.MWSafeHandle..ctor()
at MathWorks.MATLAB.NET.Arrays.MWArray..ctor()
at MathWorks.MATLAB.NET.Arrays.MWIndexArray..ctor()
at MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(MWArrayComponent arrayComponent, Int32 rows, Int32 columns)
at MathWorks.MATLAB.NET.Arrays.MWIndexArray..cctor()
InnerException:
我尝试在控制台应用程序中执行此操作,并且工作正常,但我需要使用 .dll 内置于 matlab 的 .dll。
有任何想法吗?
最佳答案
帮助我的是确保 C# 应用程序和 matlab 模块在相同的架构下 - 意思是 32 位或 64 位。
如果您的 Matlab 是 64 位 - 尝试将 C# 项目更改为 64 位
看到这个帖子:
Creating a shared library (.NET Assembly) in Matlab and using it in C#
关于c# - 在 C# 类库中使用 MATLAB MWArray.dll,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23384629/