【问题标题】:Exception when initialize dll in c# generated by MATLAB Compiler在 MATLAB 编译器生成的 c# 中初始化 dll 时出现异常
【发布时间】:2017-11-24 14:35:32
【问题描述】:

我使用 MATLAB Compiler 生成了一个.NET Assembly,只需要很少的 MATLAB 代码:

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

我使用向导生成了 dll。

在我的 Visual Studio 项目中,我添加了对生成的 dll (extest.dll) 和 MATLAB 运行时 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();
        }
    }
}

它的构建没有错误,智能感知正在工作(所以根据我的理解,所有参考都应该是好的)

但是当我启动它时,会抛出以下异常(new ExClass()):

在 DllTesting.exe 中发生“System.TypeInitializationException”类型的未处理异常

附加信息:“extest.ExClass”的类型初始化程序引发了异常。

任何建议此代码有什么问题或缺少什么?

【问题讨论】:

    标签: c# matlab dll matlab-compiler


    【解决方案1】:

    尝试在类定义之前添加它

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

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

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

    如果这些都没有帮助,请查看 herehere,您可能有 64/32 位不匹配。

    【讨论】:

    • hmm 如果我添加提到的行,我会收到以下编译器错误:Error CS1730 Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations。并且全部设置为 64 位。
    • 我刚刚做了。没有编译器错误了。但问题仍然存在。 ://
    • 嗯。您可能必须在 Matlab 上尝试不同的编译器选项。除了我已经建议的内容之外,我不确定还有什么可以推荐的。
    • 谢谢。我现在设法运行它。我再次编译了几次代码,并将 .net 框架更改为 4.6.2 的 4.0(因为它是我的默认设置)。据我了解 matlab 文档它应该适用于 4.0 及更高版本......它没有接缝。所以我不能 100% 确定是因为重新编译还是魔法:D
    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 2015-02-02
    • 2021-02-24
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多