从.net dll(C#)返回结构 [英] return struct from .net dll (C#)

查看:89
本文介绍了从.net dll(C#)返回结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net DLL(C#),它通过TCP从传感器接收实时(和大量)数据.数据以自定义对象数组的形式出现. (将其称为SensorPacket,它具有大约20个字段,大部分是float和int).

I have a .net DLL (C#) that receives real-time (and lots) of data over TCP from a sensor. The data is coming as arrays of custom objects. (call it a SensorPacket, that has about 20 fields, mostly float and int).

我一直通过事件将其发送到matlab(工作正常).但是Matlab将它们视为.NET对象的数组.我希望将其作为结构数组发送.怎么样?

I have been sending this up to matlab via events (works fine). But Matlab sees them as an array of .NET objects. I would prefer to send this up as a array of struct. How?

是的,可以通过以下方式将此数组转换为单元格数组:

yes, one can convert this array to a cell array via:

http://www.mathworks. com/help/matlab/matlab_external/net-arrays-to-cell-arrays.html

但是仍然将其保留为.net对象的单元格数组.一个人可以投射对象以便我获取值吗?

but that still leaves this as a cell array of .net objects. Can one cast the objects so that I can get the values?

最好发送一个struct的单元格/数组.

and better would be to send a cell/array of struct.

我曾尝试将其转换为JSON,并使用matlab JSON阅读器将其转换为结构,但这非常慢: http://www. mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files

I had tried converting this to JSON, and using a matlab JSON reader to convert to structs, but this is very very slow: http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files

别笑,但是我正在考虑使用DLL编写一个.MAT文件,然后将其句柄发送给Matlab.该作者可能会工作: http://www.mathworks.com/matlabcentral/fileexchange/16319-csmatio--mat-file-io-api-for-net-2-0

Don't laugh, but I am thinking of using the DLL to write a .MAT file, and then send up the handle to that to Matlab. This writer might work: http://www.mathworks.com/matlabcentral/fileexchange/16319-csmatio--mat-file-i-o-api-for-net-2-0

在下一页中,它说我可以返回一个结构或一个类,但是我的经验是,这会返回一个System.Object

In the following page, it says I can return a struct or a class, but my experience is that this returns a System.Object

http://www. mathworks.com/help/matlab/matlab_external/handling-net-data-in-matlab_bte9owt-1.html

推荐答案

好吧,看来与普通C,mex文件不同,您不能返回matlab本机结构和对象.但是您可以返回定义的类的C#对象,并且可以从Matlab访问属性(只需执行properties(netObj),然后您就会看到.确实,您将看到System.String []作为字符串,但是可以通过在对象上执行char()来固定它.大多数其他标量也可以使用.

Well, it appears that unlike plain-C, mex files, you cannot return matlab native structs and object. But you can return a C# object of a class you define, and you can access the properties from Matlab (just do a properties(netObj) and you will see then. True, you will see a System.String[] for strings, but that can be fixed by doing a char() on the object. Most others scalars also work.

在我的情况下,我通过SingnalR获取JSON消息,将它们转换为ExpandDo对象,然后创建单个类对象,并将EVENTS发送到matlab.所有这些都有效,并且比Matlab解析JSON快20倍.但是对于真正的批量工作仍然有些慢.我现在开始使用csmatio工具( https://sourceforge.net/projects/csmatio )编写一个.MAT文件,然后我将通知Matlab该数据已准备就绪.

In my case, I am getting JSON messages via SingnalR, converting them to an ExpandDo object, then creating the individual class objects, and sending EVENTS to matlab. This all works, and is 20X faster than having the Matlab parse the JSON. but still a little slow for real bulk work. I am now starting on using the csmatio tools (https://sourceforge.net/projects/csmatio )to write a .MAT file, and then I will signal Matlab that the data is ready for it.

但是下面您可以看到我的C#代码,以了解我现在的工作方式:(请注意,您可以只创建.NET事件),它们将像任何Matlab事件一样显示在Matlab中,并将值作为参数传递!我执行task.invoke,以防止在matlab处理数据时挂起.NET中的线程.

But below you can see my C# code for how I am doing it write now: (note, you can just create .NET events) and they will show up in Matlab just like any Matlab events, and pass the values as arguments! I do the task.invoke to keep the thread in .NET from hanging while matlab processes the data.

    public delegate void PostPerson(Portal sender, dynamic e);
    public event PostPerson Post;

        private void deliver(dynamic packet)
    {
        var converter = new ExpandoObjectConverter();
        dynamic data = JsonConvert.DeserializeObject<ExpandoObject>(packet.ToString(), converter);
        dynamic mail;
        String channelName = (string)data.Channel;
        switch (channelName)
        {
            case "Oxford":
                mail = new Oxford(data);
                break;
            case "RADAR":
                mail = new Radar(data);
                break;
            case "SVSRear":
                mail = new SVS(data);
                break;
            case "SVSFront":
                mail = new SVS(data);
                break;
            case "IBEO":
                mail = new IBEO(data);
                break;
            case "Fusion":
                mail = new Fusion(data);
                break;
            default:
                postFail(String.Format("Portal: deliver: Unknown Channel: {0}", channelName));
                return;
        }
        trigger(mail);
    }
    private void trigger(dynamic e)
    {
        if (Post != null)
        {
            //Post(this, e);
            Task.Factory.StartNew(() => { Post(this, e); });
        }
    }

这篇关于从.net dll(C#)返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆