gpt4 book ai didi

c# - StructLayout 仅适用于结构?

转载 作者:可可西里 更新时间:2023-11-01 07:55:59 25 4
gpt4 key购买 nike

我注意到有很多地方我们有这样的东西:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class E2H_LANEFIND_DATA_T
{
....
}

使用 StructLayout 的类定义。这样可以吗?还是 StructLayout 仅适用于 struct

最佳答案

The documentation明确指出:

You can apply this attribute to classes or structures.

Typically, the common language runtime controls the physical layout of the data fields of a class or structure in managed memory. However, if you want to arrange the class or structure needs in a certain way, you can use StructLayoutAttribute. Explicit control of a class layout is important if the class is to be passed to unmanaged code that expects a specific layout.

重点是我的。

在这种情况下要考虑的主要问题是您希望通过值还是通过引用传递实例,在这种情况下,您将分别使用结构或类。

关于c# - StructLayout 仅适用于结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3355014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com