예제 #1
0
        /// <summary>
        /// 获得指定枚举的所有值名称
        /// </summary>
        /// <param name="type">指定的类型,可以为枚举或可空枚举</param>
        /// <returns>所有值名称的集合</returns>
        /// <exception cref="T:System.ArgumentNullException">type为null</exception>
        public static HashSet <string> GetEnumNameKeys(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var enumType = FishObjectExtension.GetTypeInfo(type);

            if (enumType.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                type     = enumType.GetGenericArguments()[0];
                enumType = FishObjectExtension.GetTypeInfo(type);
            }
            if (!enumType.IsEnum)
            {
                throw new InvalidProgramException("Not an enum type.");
            }

            HashSet <string> result;

            if (!_enumNameKeysCache.TryGetValue(type, out result))
            {
                lock (_lockObject)
                {
                    if (!_enumNameKeysCache.ContainsKey(type))
                    {
                        _enumNameKeysCache.Add(type, Enum.GetNames(type).MapToHashSet(StringComparer.OrdinalIgnoreCase));
                    }
                }
                result = _enumNameKeysCache[type];
            }

            return(result);
        }
예제 #2
0
 /// <summary>
 /// 判断指定的类型是否具有指定的接口
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="t"></param>
 /// <returns></returns>
 public static bool HasInterface <T>(this Type t)
 {
     return(t != null && (FishObjectExtension.GetTypeInfo(t)).GetInterface(typeof(T).FullName) != null);
 }