我注意到有很多地方我们有这样的东西:
[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/