传递MATLAB方法作为.NET对象的委托 [英] Passing MATLAB methods as delegates to .NET Object
问题描述
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屋!