5

假设我有这个结构,

[StructLayout(LayoutKind.Explicit)]
public struct Chapter4Time
{
    [FieldOffset(0)]
    public UInt16 Unused;
    [FieldOffset(2)]
    public UInt16 TimeHigh;
    [FieldOffset(4)]
    public UInt16 TimeLow;
    [FieldOffset(6)]
    public UInt16 MicroSeconds;
}

和这个结构。

[StructLayout(LayoutKind.Explicit)]
public struct IEEE_1588Time
{
    [FieldOffset(0)]
    public UInt32 NanoSeconds;
    [FieldOffset(4)]
    public UInt32 Seconds;
}

我将如何从一种结构转换为另一种结构?

4

1 回答 1

6

这里有两个选项:

没有不安全的代码,但有明确的结构布局

(请注意,尽管就 C# 编译器而言这并不是不安全的,但某些框架可能仍然不允许它 - 请参阅 Marc Gravell 的评论。)

您可以使用联合类型,它只是另一个具有两个字段的结构,两者都显式设置为相同的位置。这是使用您的结构的完整示例:

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]
public struct Chapter4Time
{
    [FieldOffset(0)]
    public UInt16 Unused;
    [FieldOffset(2)]
    public UInt16 TimeHigh;
    [FieldOffset(4)]
    public UInt16 TimeLow;
    [FieldOffset(6)]
    public UInt16 MicroSeconds;
}

[StructLayout(LayoutKind.Explicit)]
public struct IEEE_1588Time
{
    [FieldOffset(0)]
    public UInt32 NanoSeconds;
    [FieldOffset(4)]
    public UInt32 Seconds;
}

[StructLayout(LayoutKind.Explicit)]
public struct TimeUnion
{
    [FieldOffset(0)]
    public Chapter4Time Chapter4Time;
    [FieldOffset(0)]
    public IEEE_1588Time IEEE_1588Time;
}

class Test
{    
    static void Main()
    {
        var ch4 = new Chapter4Time { TimeLow = 100, MicroSeconds = 50 };
        var union = new TimeUnion { Chapter4Time = ch4 };
        Console.WriteLine(union.IEEE_1588Time.Seconds);
    }
}

不安全的代码,转换指针

如果您可以使用不安全代码,则联合类型的替代方法是将类型指针强制转换Chapter4Time*IEEE_1588Time*

class Test
{    
    unsafe static void Main()
    {
        var ch4 = new Chapter4Time { TimeLow = 100, MicroSeconds = 50 };
        var ieee1588 = *((IEEE_1588Time*) &ch4);
        Console.WriteLine(ieee1588.Seconds);
    }
}

就我个人而言,如果可能的话,我会避免这样做,但如果你真的,真的想这样做,这些可能是最简单的方法。

于 2013-07-14T17:32:41.347 回答