Enum.IsDefine源码

public static bool IsDefined(Type enumType, object value)
{
    if (enumType == null)
    {
        throw new ArgumentNullException("enumType");
    }
    if (!(enumType is RuntimeType))
    {
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
    }
    if (!enumType.IsEnum)
    {
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
    }
    if (value == null)
    {
        throw new ArgumentNullException("value");
    }
    Type type = value.GetType();
    if (!(type is RuntimeType))
    {
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "valueType");
    }
    Type underlyingType = GetUnderlyingType(enumType);
    if (type.IsEnum)
    {
        Type type3 = GetUnderlyingType(type);
        if (type != enumType)
        {
            throw new ArgumentException(string.Format(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType"), type.ToString(), enumType.ToString()));
        }
        type = type3;
    }
    else if ((type != underlyingType) && (type != stringType))
    {
        throw new ArgumentException(string.Format(Environment.GetResourceString("Arg_EnumUnderlyingTypeAndObjectMustBeSameType"), type.ToString(), underlyingType.ToString()));
    }
    if (type == stringType)
    {
        string[] names = GetHashEntry(enumType).names;
        for (int i = 0; i < names.Length; i++)
        {
            if (names[i].Equals((string) value))
            {
                return true;
            }
        }
        return false;
    }
    ulong[] values = GetHashEntry(enumType).values;
    if ((((type != intType) && (type != typeof(short))) && ((type != typeof(ushort)) && (type != typeof(byte)))) && (((type != typeof(sbyte)) && (type != typeof(uint))) && ((type != typeof(long)) && (type != typeof(ulong)))))
    {
        throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
    }
    ulong num2 = ToUInt64(value);
    return (BinarySearch(values, num2) >= 0);
}

posted on 2010-02-09 09:34  木可  阅读(265)  评论(0编辑  收藏  举报

导航