Mapping tool (like Automapper) for Matlab

268 Views Asked by At

I've been enjoying the power of working with .NET types in Matlab using the Matlab .NET Interface.

I'm currently attempting a set of Matlab wrappers to expose a .NET API (wrapping to make the API feel "Matlab-y"). One of the things I find myself doing over and over is creating static helper methods in Matlab to translate DTOs from a .NET entity to a Matlab struct or class.

Does anyone know of an AutoMapper-like tool to help with this mapping?

Edit:

Here's an example. In a C# library:

namespace MyLib
{
    public class MyClass
    {
        public string MyString { get; set; }
        public int MyInt { get; set; }

        public MyClass(string myString, int myInt)
        {
            MyString = myString;
            MyInt = myInt;
        }
    }
}

Then, in Matlab:

NET.addAssembly('MyLib.dll');

% create an instance of my .NET type
netObject = MyLib.MyClass('high', 5);

% map that instance to values in a Matlab struct
% since Matlab's dynamic, create the struct on the fly
matlabStruct = map(netObject);

% assert that the values have been mapped correctly
assert(isstruct(matlabStruct));
assert(isfield(matlabStruct, 'MyString'));
assert(isfield(matlabStruct, 'MyInt'));
assert(matlabStruct.MyString == 'high');
assert(matlabStruct.MyInt == 5);

% equivalent code w/o mapper:
matlabStruct.MyString = char(netObject.MyString);
matlabStruct.MyInt = int32(netObject.MyInt);
0

There are 0 best solutions below