How to invoke a .NET-method which expects a .NET-struct as parameter?

1 次查看(过去 30 天)
Hello,
is this possible at all in Matlab? I have a .NET assembly with a method that expects a .NET struct as parameter. I can't figure out how to create an appropriate struct for the method. I always get the error
"No method 'TestNETNameSpace.TestNETClass.passStructToMe' with matching signature found."
with this Matlab code or my other attempts:
asmbl = NET.addAssembly('C:\Test\testNETAssembly.dll');
import TestNETNameSpace.*
result1 = TestNETClass.passIntToMe(42); % This works fine.
% Question: How to make this right?
Matlabstruct.stringInStruct = "hello";
Matlabstruct.doubleInStruct = 12.5;
result2 = TestNETClass.passStructToMe(Matlabstruct); % This throws an error.
This is the C# -code of my assembly testNETAssembly.dll.
namespace TestNETNameSpace
{
public static class TestNETClass
{
public struct TestNETStruct
{
public string stringInStruct;
public double doubleInStruct;
}
public static string passStructToMe(TestNETStruct str)
{
return str.stringInStruct;
}
public static int passIntToMe(int i)
{
return i;
}
}
}

回答(1 个)

Mohammad Sami
Mohammad Sami 2025-4-23
编辑:Mohammad Sami 2025-4-23
You can follow the type converstion between Matlab and .NET here
In particular your .NET function would need to accept the following type of variable.
MathWorks.MATLAB.Types.MATLABStruct
MathWorks.MATLAB.Types.MATLABStruct[]
MathWorks.MATLAB.Types.MATLABStruct[,,...]
If you are trying to use a third parties code, I assume you will then have to write your own wrapper functions in .NET which covert your matlab data into the type required for your third party library to work.

类别

Help CenterFile Exchange 中查找有关 Get Started with Microsoft .NET 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by