c# - StructLayout 仅适用于结构?

标签 c#

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

[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/

相关文章:

c# - 关闭C#App时是否需要清除Native C++内存

c# - VS 2013 C# 和 Git : . csproj 文件在 pull 时不 merge

c# - Telerik RadTabStrip 的问题

c# - 过滤掉搜索查询的常用词

c# - 将不同类型的对象传递给非托管函数

c# - 在代码中查找 Webview2 WPF 控件的边缘版本

c# - 防止实体模型加载本地实例

c# - UWP:DataTemplateSelector 和 SelectedItem

c# - 获得最前沿的控制

c# - 在节目中展开 C# propertygrid