Type.GetEnumUnderlyingType() 替代 .net35

Type.GetEnumUnderlyingType() replacement for .net35

我正在寻找一种方法来复制 .net35 中缺少的 Type.GetEnumUnderlyingType() 的功能。

只需使用 Enum.GetUnderlyingType 即可:

Returns the underlying type of the specified enumeration.

这是 TypeGetEnumUnderlyingType 的实现:

public virtual Type GetEnumUnderlyingType()
{
    if (!this.IsEnum)
        throw new ArgumentException(
            Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
    FieldInfo[] fields = this.GetFields(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    if (fields == null || fields.Length != 1)
        throw new ArgumentException(
            Environment.GetResourceString("Argument_InvalidEnum"), "enumType");
    return fields[0].FieldType;
}