简体   繁体   中英

Type.GetEnumUnderlyingType() replacement for .net35

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

Just use Enum.GetUnderlyingType instead:

Returns the underlying type of the specified enumeration.

Here's the implementation for GetEnumUnderlyingType from Type :

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM