.net 枚举是 blittable 类型吗? (编组)

发布于 2024-10-30 17:43:25 字数 481 浏览 0 评论 0原文

显然有一个 blittable 类型列表,到目前为止我还没有看到专门的枚举。它们一般来说是可以位块传送的吗?或者它是否取决于它们是否使用 blittable 基类型声明?

//e.g.
internal enum SERVERCALL : uint
{
    IsHandled = 0,
    Rejected = 1,
    RetryLater = 2,
}

参考文献已用尽:

Apparently there's a list of blittable types and so far I don't see Enums specifically on it. Are they blittable in general? Or does it depend on whether they are declared with a blittable base type?

//e.g.
internal enum SERVERCALL : uint
{
    IsHandled = 0,
    Rejected = 1,
    RetryLater = 2,
}

References exhausted:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-11-06 17:43:25

枚举是可直接传送的类型。来自enum关键字文档

每个枚举类型都有一个
基础类型,可以是任何
除 char 之外的整型。

因为基础类型是整型(所有类型都在 blittable 类型列表中),所以枚举也是 blittable。

Enums are blittable types. From the enum keyword documentation:

Every enumeration type has an
underlying type, which can be any
integral type except char.

Because the underlying type is integral (all of which are on the list of blittable types), the enum is also blittable.

当爱已成负担 2024-11-06 17:43:25

枚举类型本身不可blittable(因为它们在非托管世界中没有对应项),但却可以。

Enum types themselves are not blittable (since they do not have counterpart in unmanaged world) but the values are.

獨角戲 2024-11-06 17:43:25

阿利奥斯塔是正确的。例如,如果尝试执行以下语句:

int size = Marshal.SizeOf( System.ConsoleColor.Red );

则会引发 ArgumentException,并显示消息:

Type 'System.ConsoleColor' 无法封送为非托管结构;无法计算出有意义的大小或偏移量。

然而,声明:

int size = Marshal.SizeOf( (int)System.ConsoleColor.Red );

正如人们所期望的那样,效果很好。

同样,语句:

int enumSize = Marshal.SizeOf( typeof(ConsoleColor) );

失败,但语句:

int enumSize = Marshal.SizeOf( Enum.GetUnderlyingType( typeof(ConsoleColor) ) );

成功。

不幸的是,Microsoft 的 Marshal 文档。 SizeOf( object ) 不足;该页面甚至没有在可能的异常列表中包含 ArgumentExceptionMarshal.SizeOf( Type ) 列出了 ArgumentException,但仅表示当类型为泛型时抛出该异常(这是事实,但不包括上面的示例)。

(此外,的文档enum 关键字,Enum 类和 枚举类型根本没有提及枚举值是否可以直接 blittable。)

Aliostad is correct. For example, if one tries to execute the statement:

int size = Marshal.SizeOf( System.ConsoleColor.Red );

then an ArgumentException is thrown, with the message:

Type 'System.ConsoleColor' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

However, the statement:

int size = Marshal.SizeOf( (int)System.ConsoleColor.Red );

works just fine as one would expect.

Likewise, the statement:

int enumSize = Marshal.SizeOf( typeof(ConsoleColor) );

fails, but the statement:

int enumSize = Marshal.SizeOf( Enum.GetUnderlyingType( typeof(ConsoleColor) ) );

succeeds.

Unfortunately, Microsoft's documentation for Marshal.SizeOf( object ) is deficient; that page doesn't even include ArgumentException in the list of possible exceptions. The doc for Marshal.SizeOf( Type ) lists ArgumentException, but only says that it's thrown when the type is generic (which is true, but doesn't cover the above example).

(Also, the documentation for the enum keyword, the Enum class, and Enumeration Types in the C# Programming Guide makes no mention at all about whether an enum value is directly blittable.)

土豪我们做朋友吧 2024-11-06 17:43:25

而不是

int enumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof(ConsoleColor)));

你实际上可以写

int enumSize = Marshal.SizeOf(typeof(ConsoleColor).GetEnumUnderlyingType());

第一个调用第二个......

instead of

int enumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof(ConsoleColor)));

you can write

int enumSize = Marshal.SizeOf(typeof(ConsoleColor).GetEnumUnderlyingType());

actually the first one calls the second one...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文