传递MATLAB方法作为.NET对象的委托 [英] Passing MATLAB methods as delegates to .NET Object

查看:134
本文介绍了传递MATLAB方法作为.NET对象的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个使用.NET库的MATLAB项目。在Matlab中,我阅读了.dll并实例化了一个我开发的.NET对象,并将数据传递给它和/或从它传递:

  NET.addAssembly('myLibrary.dll'); 
myNetObj = myNamespace.MyClass();
myNetObj.DoWork(someMatlabVariable);

遵循MATLAB文档(参见这里)我可以将变量传递给我的.NET函数,但是我想让我的.NET代码调用matlab方法/回调。文档清楚地定义了如何使用.NET代理MATLAB到.NET代理(请参阅这里)。



是否可以传递(指向)一个MATLAB函数作为.NET代理或Action,所以我可以从我的.NET对象调用回调?

解决方案

我发现如何将Matlab函数句柄传递给c#中的代表。答案被埋在 MATLAB .NET文档中。 p>

在.NET / C#代码中:

 命名空间SomeNamespace {
//使用适当的输入参数和返回类型创建代理
public Delegate void SomeDelegateType();

public class SomeClass {
//为Matlab创建一个赋值给
public SomeDelegateType SomeDelegate;
...
}
}

在Matlab中: / p>

  //%Instantiate .NET对象
classInstance = SomeNamespace.SomeClass();
//%将MATLAB函数句柄分配给.NET Delegate
classInstance.SomeDelegate = SomeNamespace.SomeDelegateType(@SomeMatlabMethod);

函数SomeMatlabMethod()
...
end



现在,当.NET代码调用 SomeDelegate()它将调用Matlab函数 SomeMatlabMethod()句柄传递


Currently I have a MATLAB project using a .NET library. In Matlab I read in the .dll and instantiate a .NET object that I developed, and pass data to and from it:

NET.addAssembly('myLibrary.dll');
myNetObj = myNamespace.MyClass();
myNetObj.DoWork(someMatlabVariable);

Following the MATLAB documentation (See here) I can pass variables to my .NET functions, but I would like to have my .NET code call matlab methods/callbacks. The documentation clearly defines how to use .NET delegates MATLAB to .NET Delegates (See here).

Is it possible to pass (point to) a MATLAB function as a .NET Delegate or Action, so I can call the callback from my .NET object?

解决方案

I found how to pass Matlab function handles to delegates in c#. The answer is buried in the MATLAB .NET Documentation.

In .NET/C# code:

namespace SomeNamespace{
   // Create Delegate with appropriate input parameters and return types
   public Delegate void SomeDelegateType();

   public class SomeClass{
      // Create a Delegate for Matlab to assign to
      public SomeDelegateType SomeDelegate;
      ...
   }
}

In Matlab:

// % Instantiate .NET object    
classInstance = SomeNamespace.SomeClass();
// % Assign matlab function handle to .NET Delegate
classInstance.SomeDelegate = SomeNamespace.SomeDelegateType(@SomeMatlabMethod);

function SomeMatlabMethod()
   ...
end

Now when the .NET code calls SomeDelegate() it will call the Matlab function SomeMatlabMethod() handle passed to it.

这篇关于传递MATLAB方法作为.NET对象的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆