如何在C#中创建.NET组件,MATLAB可以消费?
我正在使用Visual Studio 2022在C#中编写.NET组件:
using System;
namespace ClassLibrary1
{
public class Class1
{
public int f(int i) { return 3 * i; }
}
}
当我尝试使用以下脚本将其加载到MATLAB中时:
try
NET.addAssembly(fullfile(pwd, "ClassLibrary1.dll"));
catch ex
end
ex.ExceptionObject.LoaderExceptions.Get(0).Message
我会收到错误消息:
die datei oder汇编“ system.runtime,版本= 6.0.0.0 文化=中性,publicKeyToken = B03F5F7F11D50A3A“ ODER EINE AbhängigkeitDavon Wurde Nicht Gefunden。 DAS系统Kann Die Angebene Datei Nicht Finden。
我不知道英语措辞是什么,就像
无法加载文件或汇编“ System.Runtime ....
。
系统 带有相同的错误消息。 只是为.NET核心,缺少的汇编版本是
System.Runtime,版本= 4.2.2.0
我还尝试使用C#控制台程序消费,该程序无需问题。
I am writing a .net assembly in c# using Visual Studio 2022:
using System;
namespace ClassLibrary1
{
public class Class1
{
public int f(int i) { return 3 * i; }
}
}
When I try to load it into MATLAB using the following script:
try
NET.addAssembly(fullfile(pwd, "ClassLibrary1.dll"));
catch ex
end
ex.ExceptionObject.LoaderExceptions.Get(0).Message
I get the error message:
Die Datei oder Assembly "System.Runtime, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" oder eine
Abhängigkeit davon wurde nicht gefunden. Das System kann die
angegebene Datei nicht finden.
I don't know exactly what the english wording is, something like
Could not load file or assembly "System.Runtime....
The dll-file was locked by MATLAB after this.
I tried creating the assembly using .NET 6.0 as well as .NET core 3.1 in combination with MATLAB 2021b and 2022a, all with the same error message.
Just for .NET core, the missing assembly version was
System.Runtime, Version=4.2.2.0
I also tried to consume the assembly with c# console programs, which worked without issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来MATLAB没有为现代.NET Runtimes提供支持。根据文档(已经由@mib_coder链接)matlab支持'.NET Framework版本4.0及更高版本'。这意味着.csproj文件中的
targetFramework
属性必须对应于crl(runtime)4( https://en.wikipedia.org/wiki/.net_framework_version_history )。这也意味着您不能使用为.NET Core编译的任何C#代码或.NET 5/6/7。这意味着您仅限于c#< = 7.3,但是,如果使用.NET Framework&= 4.8或.NET Standard< = 2.0编译了OP中显示的类。我使用.NET Framework 4.5和.NET Standard 2.0编译,并且都可以加载到MATLAB中。
几个评论:
Mathworks应该更新对更新和更通用的.NET Runtimes的支持。 .NET Framework是.NET的仅限Windows版本,但是我能够使用.NET Standard 2.0编译的代码的事实告诉我,有一些希望可以在其他OSS上使用该版本,但是根据文档页面仅支持Windows。
根据Microsoft ,.NET Standard 2.1(我目前尚未安装)支持C#< = 8.0,因此,如果在MATLAB Interop中起作用,您至少将具有8.0带有的一些不错的功能(
使用
语句,无效的参考类型等)。要安装CRL 4,您需要安装与此版本相对应的.NET SDK。开发人员包和运行时间可供下载在这里。 /p>
It appears that Matlab does not offer support for modern .NET runtimes. According to the documentation (already linked by @MiB_Coder) Matlab supports '.NET Framework Version 4.0 and above'. This means the
TargetFramework
property in your .csproj file must correspond to CRL (runtime) 4 (https://en.wikipedia.org/wiki/.NET_Framework_version_history).This also means you can't use any C# code compiled for .NET Core, or .NET 5/6/7. This means that you are limited to C# <= 7.3 You should, however, be able to summon the class shown in the OP if it is compiled compiled using .NET Framework <= 4.8, or .NET Standard <= 2.0. I compiled using .NET Framework 4.5 and .NET Standard 2.0 and both loaded into Matlab just fine.
A couple of comments:
Mathworks should update their support for newer and more universal .NET Runtimes. .NET Framework is a Windows-only version of .NET, but the fact that I was able to use code compiled against .NET Standard 2.0 tells me there is some hope that that version could be used on other OSs, but according to the documentation page only Windows is supported.
According to Microsoft, .NET Standard 2.1 (which I don't currently have installed) supports C# <= 8.0, so if this works in Matlab interop, you will at least have some of the nice features that came in with 8.0 (
using
statements, nullable reference types, etc.).To install CRL 4, you need to install a .NET SDK corresponding to this release. The developer packs and runtimes are available for download here.