从MATLAB发布.NET程序集

Release a .NET assembly from MATLAB [duplicate]
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我正在与C#dll库进行接口的小型MATLAB项目.MATLAB文档允许您按以下说明加载程序集:http : //uk.mathworks.com/help/matlab/ref/net.addassembly.html? s_tid = gn_loc_drophttp://uk.mathworks.com/help/ matlab / matlab_external / an-assembly-is-a-library-of-net-classes.html

例如,我的电话如下:

NET.addAssembly('C:\MyLibrary.dll');

到这里一切都很好,我什至可以通过附加到MATLAB进程在VS中进行调试,但是存在一个小问题.没有"removeAssembly"方法可以将程序集与MATLAB进程断开连接.如果要在VS中进行更改,则必须重新启动MATLAB才能解锁DLL,这实际上是一个麻烦.我什至尝试使用解锁软件,但是运气不好.

有人知道如何处理吗?

解决过程1

无法卸载装配,这是已知的限制之一

速聊1:
哦,不...尽管如此,感谢您的协助
解决过程2

您可以使用系统调用和Matlab命令行选项从Matlab本身启动新的Matlab实例.然后,您可以在新实例中测试.NET库.测试后关闭新实例时,.NET库将被释放.通过这种解决方法,您无需完全重新打开Matlab.不要在主Matlab实例中加载.NET程序集.

Windows PC上打开一个新的Matlab实例(整个IDE GUI):

log_str = '-logfile "C:\Temp\logfile.txt"';
system_str = [ 'matlab.exe -nosplash ' log_str ];
system(system_str);

通过自动在新的Matlab实例中执行m文件-r,而无需使用IDE GUI,-nodesktop并在测试后关闭新实例:

log_str = '-logfile "C:\Temp\logfile.txt"';
my_file = 'C:\Temp\test.m';
system_str = [ 'matlab.exe -nosplash -nodesktop -r "run(''' my_file '''), pause(2), exit" ' log_str ];
system(system_str);

日志文件记录Matlab实例的控制台输出.它是可选的,但有助于测试.如果您希望子实例是异步的(即将句柄返回到Matlab主实例),则可以' &'在的末尾添加system_str.

I am working on a small MATLAB project interfacing with a C# dll library. MATLAB documentation allows you to load an assembly as described here: http://uk.mathworks.com/help/matlab/ref/net.addassembly.html?s_tid=gn_loc_drop and http://uk.mathworks.com/help/matlab/matlab_external/an-assembly-is-a-library-of-net-classes.html

and my call for example is as follows:

NET.addAssembly('C:\MyLibrary.dll');

Everything is fine up to here, I can even debug in VS by attaching to MATLAB process, however there is a small issue. There is no 'removeAssembly' method to disconnect the assembly from MATLAB process. If I want to make a change in VS, I have to restart MATLAB to unlock the DLL which is really a pain in the b~~t. I even tried to use unlocker software with no luck..MATLAB freezes.

Does anyone know how to deal with this?

Solutions1

Unloading an assembly is not possible, it's one of the known limitations

Talk1:
oh no...anyway though thanks for your assistance
Solutions2

You can start a new Matlab instance from Matlab itself with a system call and the Matlab command line options. Then you can test your .NET libraries within the new instance. When the new instance is closed after your test, the .NET libraries are released. With this workaround, you don't need to reopen Matlab completely. Do not load the .NET assembly in the main Matlab instance.

Open a new Matlab instance (whole IDE GUI) on a Windows PC:

log_str = '-logfile "C:\Temp\logfile.txt"';
system_str = [ 'matlab.exe -nosplash ' log_str ];
system(system_str);

Automatically execute an m-File in the new Matlab instance via -r, without the IDE GUI with -nodesktop and have the new instance close itself after your test:

log_str = '-logfile "C:\Temp\logfile.txt"';
my_file = 'C:\Temp\test.m';
system_str = [ 'matlab.exe -nosplash -nodesktop -r "run(''' my_file '''), pause(2), exit" ' log_str ];
system(system_str);

The logfile logs the console output of the Matlab instance. It is optional, but helpful for testing. If you want your child instances to be asynchronous (i.e. returning the handle to the main Matlab instance), you can add ' &' at the end of the system_str.

转载于:https://stackoverflow.com/questions/28362596/release-a-net-assembly-from-matlab

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入