Enum.IsDefined搜索名称和值的原因和方式(内部)



假设我们已经定义了Planets枚举:

public enum Planets
{
    Sun = 0,
    Mercury=5,          
    Venus,              
    Earth,          
    Jupiter,        
    Uranus,         
    Neptune   
}

我使用Enum.IsDefined方法来查找枚举中是否存在字符串。

Enum.IsDefined(typeof(Planets), "Mercury"); // result is true

但是,后来我尝试了一下,结果也是真的:

Enum.IsDefined(typeof(Planets), 5); // result is true again

怎么来的?此方法没有任何过载。它只有一个签名:

Enum.IsDefined(Type enumType, object value);

Enum.IsDefined为什么以及如何搜索名称和值?这对我来说真的很有趣,他们为什么选择那样的方式?IMO制造过载将是更好的选择,不是吗?

来自Enum.IsDefined方法

值参数可以是以下任何一个

  • enumType类型的任何成员
  • 一个变量,其值是enumType类型的枚举成员
  • 枚举成员名称的字符串表示形式。字符串中的字符的大小写必须与枚举的大小写相同成员名称
  • enumType的基础类型的值

我相信这就是为什么它没有过载并将object作为第二个参数的原因。由于此方法将object作为第二个参数,并且object是所有.NET类型的基类,因此可以传递stringint

下面是这个方法是如何实现的;

public static bool IsDefined(Type enumType, Object value)
{
     if (enumType == null)
         throw new ArgumentNullException("enumType");                    
     return enumType.IsEnumDefined(value);
}

看起来这个虚拟的Type.IsEnumDefined方法在它的实现中处理了所有这些情况:;

    public virtual bool IsEnumDefined(object value)
    {
        if (value == null)
            throw new ArgumentNullException("value");
        if (!IsEnum)
            throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
        Contract.EndContractBlock();
        // Check if both of them are of the same type
        Type valueType = value.GetType();
        // If the value is an Enum then we need to extract the underlying value from it
        if (valueType.IsEnum)
        {
            if (!valueType.IsEquivalentTo(this))
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", valueType.ToString(), this.ToString()));
            valueType = valueType.GetEnumUnderlyingType();
        }
        // If a string is passed in
        if (valueType == typeof(string))
        {
            string[] names = GetEnumNames();
            if (Array.IndexOf(names, value) >= 0)
                return true;
            else
                return false;
        }
        // If an enum or integer value is passed in
        if (Type.IsIntegerType(valueType))
        {
            Type underlyingType = GetEnumUnderlyingType();
            // We cannot compare the types directly because valueType is always a runtime type but underlyingType might not be.
            if (underlyingType.GetTypeCodeImpl() != valueType.GetTypeCodeImpl())
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumUnderlyingTypeAndObjectMustBeSameType", valueType.ToString(), underlyingType.ToString()));
            Array values = GetEnumRawConstantValues();
            return (BinarySearch(values, value) >= 0);
        }
    }

在Enum.IsDefined的文档中,它声明该值为

类型:System.Object

enumType中常量的值或名称。

您已经给Mercury提供了5的值,因此它能够看到这个

最新更新