lundi 21 octobre 2019

Invoke a matlab method in C# code with parameters

I am trying to invoke a matlab method in my C# code with parameters. I used reflection to load a dll with the matlab funcion at runtime into my application, which works fine:

Assembly matlabAssembly = Assembly.LoadFrom(info.FullName);

List<Type> types = new List<Type>();

types = matlabAssembly.GetTypes().ToList();

List<MethodInfo> methods = new List<MethodInfo>();
methods.AddRange(types[0].GetMethods());

dynamic dynamicObject = Activator.CreateInstance(types[0]);

The dll contains one type with one function:

MWArray MyMatlabFunction(MWArray, MWArray, MWArray, MWArray);

I create a few arrays an want to pass the as parameters to this function.

MWArray array1 = new MWNumericArray(120);
MWArray array2 = new MWNumericArray(100);
MWArray array3 = new MWNumericArray(15);
MWArray array4 = new MWLogicalArray(true);
object[] params = new object[] {array1, array2, array3, array4};

MethodInfo matlabFuncion = methods[5]; //MyMatlabFunction

matlabFunction.Invoke(dynamicObject, params);

When i call the invoke method, I get an exception that an MWNummericArray cannot be converted into an MWArray although MWNummericArray directly derives from MWArray. Am I missing something or am I doing it completely the wrong way?





Aucun commentaire:

Enregistrer un commentaire