2

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);
lightw8
  • 3,262
  • 2
  • 25
  • 35
  • 1
    Can you give an example? – Andrey Rubshtein Jan 16 '12 at 05:45
  • What's wrong with using Automapper? – Rami A. Jan 17 '12 at 07:06
  • 1
    Rami - AFAICT Automapper is a .NET library, and I'm interested in a Matlab tool. – lightw8 Jan 18 '12 at 19:22
  • Andrey - good point. See the updated code. Thanks, David – lightw8 Jan 18 '12 at 19:53
  • I am looking for a solution that will help me with mapping C# types to MatLab MWArray derived types (MWStructArray, MWCharArray etc) The code I write passes JSON string to MatLab library (compiled .NET MatLab code I call from my C# Web API) and MatLab deserializes it to it's native types. This process takes very long time (up to 30secs sometimes depends on complexity of JSON) and I was thinking about making Web API (C#) doing parsing job and passing MWArray object to MatLab. I earched web back and forth with little success. – Mariusz Jan 12 '17 at 11:21

0 Answers0