c# - Enum.GetUnderlyingType(Type) 是做什么的?

标签 c# .net enums

我不明白 Enum.GetUnderlyingType(Type enumType) 的用途

MSDN documentation也没有帮助:

Returns the underlying type of the specified enumeration.

这似乎将指定类型的 enum 转换为……其他东西。 o_O

什么底层类型?这看起来像是实现的一些内部细节。为什么这是公开的?我为什么要关心实现?浏览实际实现也无济于事,该方法只是进行一些检查然后调用

[MethodImplAttribute(MethodImplOptions.InternalCall)] 
private static extern Type InternalGetUnderlyingType(Type enumType);

...我找不到来源。

任何人都可以阐明这一点吗?

最佳答案

请注意,您可以通过以下方式指定枚举的基础类型

enum Foo : long { One, Two };

然后 GetUnderlyingType 将为 typeof(Foo) 返回 long

请注意,基础类型可以是除 char 类型之外的任何整数类型。

关于c# - Enum.GetUnderlyingType(Type) 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4594954/

相关文章:

swift - 使用 'case'模式匹配直接返回结果

c++ - 带有枚举解释的隐式非运算符(!)?

swift - 让或结构与枚举

c# - Roslyn 可以将 C# 编译为 CIL

c# - 在 Caliburn.micro 中设置初始窗口大小

c# - 使用 .NET 在 mySQL 中保存机器人状态数据

.net - 是否可以在同一解决方案下创建 2 个基于服务总线队列触发的 Azure 函数,并且两个队列不同?

c# - 将内容复制到流时出现 System.Net.Http.HttpRequestException 错误

c# - 在 C# 中从数据库加载组合框

c# - 什么时候返回 IOrderedEnumerable?