动态加载MATLAB生成的dll并反射dll里的函数,实现多返回值的功能

MyFun(Program实现多返回值的功能).m文件

function y=MyFun(x1,x2)
y=cell(2);
x1=1;
x2='哈哈哈';
y{1}=x1;
y{2}=x2;
end



Program实现多返回值的功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MathWorks.MATLAB.NET.Arrays;
using System.Reflection;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly ass;
            Type type;
            Object obj;


            ass = Assembly.LoadFile(@"F:\Matlab\CTest5\Mydll\for_redistribution_files_only\Mydll.dll");//加载MATLAB生成的dll文件所在绝对路径
            type = ass.GetType("Mydll.MyClass");//NameSpace.Claas(MATLAB生成dll时所起的命名空间和类的名字,命名要统一,使代码封闭)
            var method = type.GetMethod("MyFun", new Type[] { typeof(MWNumericArray), typeof(MWNumericArray) });
            int a = 3, b;
            string str1 = "什么", str2;
            obj = ass.CreateInstance("Mydll.MyClass");//生成MATLAB类的实例            
            var answer = method.Invoke(obj, new object[] { (MWNumericArray)a, (MWCharArray)str1 });

            MWCellArray output = (MWCellArray)answer; //返回值类型不同所以,MATLAB中返回值定义为cell类型,此时也只能转化为cell类型 
            var u1 = output[1];
            var u2 = output[2];
            b = ((MWNumericArray)u1).ToScalarInteger();
            str2 = ((MWCharArray)u2).ToString();         
        }
    }
}
 
 
 
 
参考资料:
http://www.cnblogs.com/rijing2004/archive/2007/08/07/reflection.html





猜你喜欢

转载自blog.csdn.net/qinglongqishi1/article/details/51206930