Ejemplo n.º 1
0
 /// <summary>
 /// 获取指定枚举类型的所有枚举字段信息。
 /// </summary>
 /// <param name="source">要获取其枚举字段信息的枚举。</param>
 /// <param name="ignoreDefaultDisplayName">指定是否忽略默认显示名称的枚举字段。</param>
 /// <returns>描述 <paramref name="source" /> 中所有枚举字段信息的 <see cref="T:YuLinTu.EnumField" /> 数组。</returns>
 public static EnumField[] GetFields(this Enum source, bool ignoreDefaultDisplayName = false)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     return(EnumField.GetFields(source.GetType(), ignoreDefaultDisplayName));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取枚举常数值的相关信息。
        /// </summary>
        /// <param name="enumValue">枚举常数值。</param>
        /// <returns>一个 <see cref="T:YuLinTu.EnumField" /> 的实例,它包含枚举常数值的一些信息。</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="enumValue" /> 为 <c>null</c>。</exception>
        /// <exception cref="T:System.ArgumentException"><paramref name="enumValue" /> 既不是枚举类型,也不是枚举基础类型,如 Int32。</exception>
        public static EnumField GetField(object enumValue)
        {
            if (enumValue == null)
            {
                throw new ArgumentNullException("enumValue");
            }
            Type type = enumValue.GetType();

            if (!type.IsEnum)
            {
                throw new ArgumentException("传入的值必须是枚举的枚举基或基础类型,如 Int32。");
            }
            EnumField[] fields = EnumField.GetFields(type, false);
            foreach (EnumField enumField in fields)
            {
                if (enumField.Value.Equals(enumValue))
                {
                    return(enumField);
                }
            }
            return(new EnumField(enumValue, enumValue.ToString(), string.Empty));
        }