我不明白 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/