.net 枚举是 blittable 类型吗? (编组)
显然有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
枚举是可直接传送的类型。来自
enum
关键字文档:因为基础类型是整型(所有类型都在 blittable 类型列表中),所以枚举也是 blittable。
Enums are blittable types. From the
enum
keyword documentation:Because the underlying type is integral (all of which are on the list of blittable types), the enum is also blittable.
枚举类型本身不可blittable(因为它们在非托管世界中没有对应项),但值却可以。
Enum types themselves are not blittable (since they do not have counterpart in unmanaged world) but the values are.
阿利奥斯塔是正确的。例如,如果尝试执行以下语句:
则会引发 ArgumentException,并显示消息:
Type 'System.ConsoleColor' 无法封送为非托管结构;无法计算出有意义的大小或偏移量。
然而,声明:
正如人们所期望的那样,效果很好。
同样,语句:
失败,但语句:
成功。
不幸的是,Microsoft 的
Marshal 文档。 SizeOf( object )
不足;该页面甚至没有在可能的异常列表中包含ArgumentException
。Marshal.SizeOf( Type )
列出了ArgumentException
,但仅表示当类型为泛型时抛出该异常(这是事实,但不包括上面的示例)。(此外,
的文档enum
关键字,Enum
类和 枚举类型根本没有提及枚举值是否可以直接 blittable。)Aliostad is correct. For example, if one tries to execute the statement:
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:
works just fine as one would expect.
Likewise, the statement:
fails, but the statement:
succeeds.
Unfortunately, Microsoft's documentation for
Marshal.SizeOf( object )
is deficient; that page doesn't even includeArgumentException
in the list of possible exceptions. The doc forMarshal.SizeOf( Type )
listsArgumentException
, 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, theEnum
class, and Enumeration Types in the C# Programming Guide makes no mention at all about whether an enum value is directly blittable.)而不是
你实际上可以写
第一个调用第二个......
instead of
you can write
actually the first one calls the second one...