假设我有这种结构,
[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;
}
我如何从一个结构转换为另一个结构?
答案 0 :(得分:5)
这里有两个选项:
没有不安全的代码,但显式的结构布局
(请注意,尽管就C#编译器而言这并不安全,但某些框架可能仍然不允许它 - 请参阅Marc Gravell的评论。)
你可以使用一个union类型,它只是另一个带有两个字段的结构,它们都明确地设置在同一个位置。这是使用您的结构的完整示例:
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);
}
}
就个人而言,如果可能的话,我会避免这样做,但如果你真的,真的想要这样做,这些可能是最简单的方法。