none
c++结构体翻译为c#结构体 RRS feed

  • 问题

  • c++原结构体如下

    typedef     struct {                                                    /* CAN_MSG                      */
        union {
            UINT8    id[4];
            UINT32    identifier;   
        };
        union {
            struct {
                UINT8    length    :4;                                      /* data length                  */
                UINT8    resbit    :2;
                UINT8    remote    :1;                                      /* remote transmission request  */
                UINT8    format    :1;                                      /* frame format                 */
            };
            UINT8    info;
        };
        UINT8    reserve[3];
        UINT8    data[8];                                                   /* data field                   */
    } CAN_MSG;

    2010年12月22日 2:56

答案

  • lxg23 你好,

    欢迎来到MSDN论坛!

    以下是改写后的C#代码,注释部分是在C#代码中需要注意的地方:

    using System.Runtime.InteropServices;

     

    public class CAN_MSG // CAN_MSG

    {

    //Unions are not supported in C#, but the following union can be simulated with the StructLayout and FieldOffset attributes.

    //ORIGINAL LINE: union

    //Structs must be named in C#, so the following struct has been named AnonymousStruct:

          [StructLayout(LayoutKind.Explicit)]

          public struct AnonymousStruct

          {

                [FieldOffset(0)]

                public byte[] id = new byte[4];

                [FieldOffset(0)]

                public uint identifier;

          }

    //Unions are not supported in C#.

    //ORIGINAL LINE: union

    //Structs must be named in C#, so the following struct has been named AnonymousStruct2:

          public struct AnonymousStruct2

          {

    //Classes must be named in C#, so the following class has been named AnonymousClass:

                public class AnonymousClass

                {

    //C# does not allow bit fields:

                      public byte length :4; // data length

                      public byte resbit :2;

                      public byte remote :1; // remote transmission request

                      public byte format :1; // frame format

                }

                public byte info;

          }

          public byte[] reserve = new byte[3];

          public byte[] data = new byte[8]; // data field

    }

     

    希望这些内容对你有所帮助

    谢谢你对MSDN论坛的支持!

    Mio

     


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     
    • 已标记为答案 lxg23 2010年12月24日 6:33
    2010年12月23日 11:22
    版主

全部回复

  • c++原结构体如下

    typedef     struct {                                                    /* CAN_MSG                      */
        union {
            UINT8    id[4];
            UINT32    identifier;   
        };
        union {
            struct {
                UINT8    length    :4;                                      /* data length                  */
                UINT8    resbit    :2;
                UINT8    remote    :1;                                      /* remote transmission request  */
                UINT8    format    :1;                                      /* frame format                 */
            };
            UINT8    info;
        };
        UINT8    reserve[3];
        UINT8    data[8];                                                   /* data field                   */
    } CAN_MSG;

    以上是C++的结构体我想在c#中用怎摸改写呢?求助 !!!
    2010年12月22日 6:15
  • Union 的话需要用 FieldOffset 去显式制定元素偏移值。您可以多试试看。

    [StructLayout(StructLayout.Sequential)]
    public struct CanMessage
    {
        [FieldOffset(0)]
        public sbyte[4] Id;

        [FieldOffset(0)]
        public int Identifier;
        ...
    }


    Mark Zhou
    2010年12月22日 9:25
  • lxg23 你好,

    欢迎来到MSDN论坛!

    以下是改写后的C#代码,注释部分是在C#代码中需要注意的地方:

    using System.Runtime.InteropServices;

     

    public class CAN_MSG // CAN_MSG

    {

    //Unions are not supported in C#, but the following union can be simulated with the StructLayout and FieldOffset attributes.

    //ORIGINAL LINE: union

    //Structs must be named in C#, so the following struct has been named AnonymousStruct:

          [StructLayout(LayoutKind.Explicit)]

          public struct AnonymousStruct

          {

                [FieldOffset(0)]

                public byte[] id = new byte[4];

                [FieldOffset(0)]

                public uint identifier;

          }

    //Unions are not supported in C#.

    //ORIGINAL LINE: union

    //Structs must be named in C#, so the following struct has been named AnonymousStruct2:

          public struct AnonymousStruct2

          {

    //Classes must be named in C#, so the following class has been named AnonymousClass:

                public class AnonymousClass

                {

    //C# does not allow bit fields:

                      public byte length :4; // data length

                      public byte resbit :2;

                      public byte remote :1; // remote transmission request

                      public byte format :1; // frame format

                }

                public byte info;

          }

          public byte[] reserve = new byte[3];

          public byte[] data = new byte[8]; // data field

    }

     

    希望这些内容对你有所帮助

    谢谢你对MSDN论坛的支持!

    Mio

     


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     
    • 已标记为答案 lxg23 2010年12月24日 6:33
    2010年12月23日 11:22
    版主
  • 谢谢我马上去试试
    2010年12月24日 6:33