为什么LayoutKind.Sequential工作不同,如果一个结构中包含日期时间字段? [英] Why does LayoutKind.Sequential work differently if a struct contains a DateTime field?

查看:149
本文介绍了为什么LayoutKind.Sequential工作不同,如果一个结构中包含日期时间字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么LayoutKind.Sequential工作不同,如果一个结构中包含日期时间字段?

Why does LayoutKind.Sequential work differently if a struct contains a DateTime field?

考虑以下code(一个控制台应用程序必须与编译不安全启用):

Consider the following code (a console app which must be compiled with "unsafe" enabled):

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication3
{
    static class Program
    {
        static void Main()
        {
            Inner test = new Inner();

            unsafe
            {
                Console.WriteLine("Address of struct   = " + ((int)&test).ToString("X"));
                Console.WriteLine("Address of First    = " + ((int)&test.First).ToString("X"));
                Console.WriteLine("Address of NotFirst = " + ((int)&test.NotFirst).ToString("X"));
            }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Inner
    {
        public byte First;
        public double NotFirst;
        public DateTime WTF;
    }
}

现在,如果我跑上面的code,我得到类似以下的输出:

Now if I run the code above, I get output similar to the following:

的结构= 40F2CC结果地址
首先地址= 40F2D4结果
NotFirst = 40F2CC地址

Address of struct = 40F2CC
Address of First = 40F2D4
Address of NotFirst = 40F2CC

注意,第一次的地址是不一样的结构的地址;然而,NotFirst的地址的的相同结构的地址。

Note that the address of First is NOT the same as the address of the struct; however, the address of NotFirst is the same as the address of the struct.

现在注释掉WTF日期时间字段的结构,并再次运行它。
这一次,我得到类似这样的输出:

Now comment out the "DateTime WTF" field in the struct, and run it again. This time, I get output similar to this:

的结构= 15F2E0结果地址
首先地址= 15F2E0结果
NotFirst = 15F2E8地址

Address of struct = 15F2E0
Address of First = 15F2E0
Address of NotFirst = 15F2E8

现在的第一的确实有相同的地址与结构。

Now "First" does have the same address as the struct.

我发现给出了使用LayoutKind.Sequential的这种行为令人吃惊。任何人都可以提供解释?用C互操作做什么时候这种行为有什么后果使用COM DATETIME类型/ C ++结构?

I find this behaviour surprising given the use of LayoutKind.Sequential. Can anyone provide an explanation? Does this behaviour have any ramifications when doing interop with C/C++ structs that use the Com DATETIME type?

注:我已经验证,当你使用Marshal.StructureToPtr()当元帅的结构,数据的以正确的顺序整理,用第一字段为先。这似乎表明,它会正常工作与互操作。奥秘就是为什么内部布局的变化 - 但当然,内部布局从来没有指定,所以编译器可以做它喜欢

NOTE: I have verified that when you use Marshal.StructureToPtr() to marshal the struct, the data is marshalled in the correct order, with the "First" field being first. This seems to suggest that it will work fine with interop. The mystery is why the internal layout changes - but of course, the internal layout is never specified, so the compiler can do what it likes.

移除结构声明不安全(它是从一些测试我是做吃剩的)。

Removed "unsafe" from struct declaration (it was leftover from some testing I was doing).

原始源出于此问题是从MSDN C#论坛:

The original source for this question was from the MSDN C# forums:

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/fb84bf1d-d9b3-4e91-823e-988257504b30\">http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/fb84bf1d-d9b3-4e91-823e-988257504b30

推荐答案

要回答我自己的问题(如建议):

To answer my own questions (as advised):

问:这是否与行为使用COM DATETIME型C / C ++结构互操作时做有什么后果?

Question: "Does this behaviour have any ramifications when doing interop with C/C++ structs that use the Com DATETIME type?"

答:没有,使用时编组站因为布局是尊重。 (我证实了这一经验。)

Answer: No, because the layout is respected when using Marshalling. (I verified this empirically.)

问题的任何人都可以提供解释?

Question "Can anyone provide an explanation?".

答:我仍然不知道这一点,但由于结构的内部重新presentation没有定义,编译器可以做什么它喜欢

Answer: I'm still not sure about this, but since the internal representation of a struct is not defined, the compiler can do what it likes.

这篇关于为什么LayoutKind.Sequential工作不同,如果一个结构中包含日期时间字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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