简体   繁体   English

C#将数据数组拆分为几个属性

[英]C# splitting array of data into several properties

I would like to get an opinion of what would be the best approach how to tackle my problem. 我想就如何解决我的问题的最佳方法提出意见。 I have some ideas but they are far from perfect in my opinion and maybe someone can suggest a nice and clean way how to do this. 我有一些想法,但我认为它们远非完美,也许有人可以建议一个好方法。

The picture below illustrates my general code structure 下图说明了我的一般代码结构 在此处输入图片说明

I have a communications module that writes data to an array consisting of data bytes. 我有一个通信模块,该模块将数据写入由数据字节组成的数组。 And i want to keep the structure there as it is, because then it is a flexible fully GUI independent solution. 我想保持结构不变,因为那样的话,它是一种灵活的完全独立于GUI的解决方案。 Just to explain my reason behind this further - the data structure represents a physical memory of an external hardware unit (MCU). 只是为了进一步解释我的原因-数据结构表示外部硬件单元(MCU)的物理内存。

After that I need to split up that data into properties. 之后,我需要将数据拆分为属性。 Meaning that I have several properties that are bound to elements in GUI giving them data. 这意味着我有一些属性绑定到GUI中的元素,从而为它们提供数据。 Thus I want each property take data from specific region in array as example index 100:104. 因此,我希望每个属性都将数组中特定区域的数据作为示例索引100:104。

The problematic part for me is how to bind these properties to the array in the mentioned way? 对我来说有问题的部分是如何以上述方式将这些属性绑定到数组? The binding needs to be two way. 绑定需要两种方式。

I am not sure if it is what you need, but looks like you can use such approach: 我不确定这是否是您需要的,但是看起来您可以使用以下方法:

You can create some structure that realize properties: 您可以创建一些实现属性的结构:

[StructLayout(LayoutKind.Explicit, Size = 11, Pack = 0)]
public struct MyStructure
{
    public string StringFromBytes
    {
        get
        {
            if (ByteArrayField == null || ByteArrayField.Length == 0)
            {
                return string.Empty;
            }

            return Utilitites.BytesToString(ByteArrayField);
        }
    }

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] [FieldOffset(0)] public byte[] ByteArrayField;
    [MarshalAs(UnmanagedType.U2)] [FieldOffset(8)] public ushort WordField;
    [MarshalAs(UnmanagedType.I1)] [FieldOffset(10)] public sbyte dBm0;
}

Here we have a structure that consist of 11 bytes. 在这里,我们有一个由11个字节组成的结构。 This structure has a property that makes some manipulation with data. 该结构具有对数据进行一些处理的属性。 You need to determine where a field starts (FieldOffset()) and how data should be interpreted (eg UnmanagedType.U2 - 2-byte unsigned integer). 您需要确定字段的起始位置(FieldOffset())以及应如何解释数据(例如UnmanagedType.U2-2字节无符号整数)。

When you receive bytes array from your device, you can easily transform that array to your structure: 当您从设备接收字节数组时,可以轻松地将该数组转换为您的结构:

    public static T ToStructure<T>(this byte[] bytes) where T : struct
    {
        GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
        var stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        handle.Free();
        return stuff;
    }

Usage: 用法:

byte[] responseBytes = Utilitites.GetResponseFromDevice();
MyStructure response = responseDecodedBytes.ToStructure<MyStructure>();

Hope that will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM