示例#1
0
        /// <summary>
        /// 枚举中是否包含,相比于Enum.IsDefine,在字符串比较时区分大小写
        /// </summary>
        /// <typeparam name="T">指定的枚举类型</typeparam>
        /// <param name="enumValue">枚举实例</param>
        /// <returns></returns>
        /// enum Permission
        /// {
        ///     Normal,
        ///     Vip
        /// }
        /// Enum.IsDefined(typeof(Permission),"Normal")   true
        /// Enum.IsDefined(typeof(Permission), "normal") false
        /// ATEnum.Constains<Permission>("normal") true
        public static bool Constains <T>(object enumValue)
        {
            Type valueType = enumValue.GetType();

            if (valueType == typeof(string))
            {
                string[] names = ATEnum.GetNames <T>();
                string   value = enumValue.ToString().ToLower();
                for (int i = 0; i < names.Length; i++)
                {
                    if (value.Equals(names[i].ToLower()))
                    {
                        return(true);
                    }
                }
            }

            if (valueType.IsIntegerType())
            {
                return(ATEnum.BinarySearch <T>(ATEnum.GetValues <T>(), enumValue) >= 0);
            }

            ATLog.Error("参数类型错误!" + ATStackInfo.GetCurrentStackInfo());
            throw new ArgumentException("参数类型错误!" + ATStackInfo.GetCurrentStackInfo());
        }
示例#2
0
        /// <summary>
        /// 获取枚举中所有成员名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string[] GetNames <T>()
        {
            Type type = typeof(T);

            if (!type.GetTypeInfo().IsEnum)
            {
                ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
                throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
            }

            return(Enum.GetNames(type));
        }
示例#3
0
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="obj">成员名称或值</param>
        ///
        /// //声明
        /// enum TestEnum
        /// {
        ///     A = 0
        /// }
        ///
        /// 示例调用:
        /// TestEnum type = ATEnum.Parse<TestEnum>("A");
        ///
        /// <returns>obj对应枚举实例中的成员</returns>
        public static T GetValue <T>(object obj)
        {
            Contract.Requires(obj != null);
            Contract.Requires(Contract.Result <T>() != null);
            Type type = typeof(T);

            if (!type.GetTypeInfo().IsEnum)
            {
                ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
                throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
            }

            string value = obj.ToString();

            return((T)Enum.Parse(type, value, true));
        }
示例#4
0
        /// <summary>
        /// 二分法查找值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int BinarySearch <T>(Array array, object value)
        {
            Type type = typeof(T);

            if (!type.GetTypeInfo().IsEnum)
            {
                ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
                throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo());
            }

            ulong[] ulArray = new ulong[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                ulArray[i] = ToUInt64(array.GetValue(i));
            }

            ulong ulValue = ToUInt64(value);

            return(Array.BinarySearch(ulArray, ulValue));
        }