private string GetEnumDescription(System.Enum objEnum) { var fieldInfo = objEnum.GetType().GetField(objEnum.ToString()); var attributeArray = fieldInfo.GetCustomAttributes(false); if (attributeArray.Length == 0) { return(objEnum.ToString()); } else { DescriptionAttribute descAttribute = null; foreach (var atb in attributeArray) { if (atb is DescriptionAttribute) { descAttribute = atb as DescriptionAttribute; } } if (descAttribute != null) { return(descAttribute.Description); } return(objEnum.ToString()); } }
public static string GetEnumDescription(Enum enumValue) { FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); List<DescriptionAttribute> attributes = fieldInfo.GetCustomAttributes<DescriptionAttribute>(false).ToList(); return attributes.Count > 0 ? attributes[0].Description : enumValue.ToString(); }
public static string GetStringValue(Enum value) { string output; var type = value.GetType(); if (StringValues.ContainsKey(value)) { output = StringValues[value].Value; } else { var fi = type.GetField(value.ToString()); var attrs = fi.GetCustomAttributes(typeof (StringValueAttribute), false) as StringValueAttribute[]; if (attrs != null && attrs.Length > 0) { output = attrs[0].Value; // Put it in the cache. lock(StringValues) { // Double check if (!StringValues.ContainsKey(value)) { StringValues.Add(value, attrs[0]); } } } else { return value.ToString(); } } return output; }
public static object GetDefaultValue(System.Enum value) { var field = value.GetType().GetField(value.ToString()); var values = field.GetCustomAttributesOfType <DefaultValueAttribute>().ToArray(); return(!values.Any() ? value.ToString() : values.First().Value); }
public static string ObterDescricao(System.Enum value) { FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return((attributes.Length > 0) ? attributes[0].Description : value.ToString()); }
public static string GetDescription(System.Enum value) { var field = value.GetType().GetField(value.ToString()); var descriptions = field.GetCustomAttributesOfType <DescriptionAttribute>().ToArray(); return(!descriptions.Any() ? value.ToString() : descriptions.First().Description); }
public static string GetDescription(this System.Enum value) { return(!(value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .SingleOrDefault() is DescriptionAttribute attribute) ? value.ToString() : attribute.Description); }
public static string GetDescription(this System.Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return(attributes.Length > 0 ? attributes[0].Description : value.ToString()); }
public static string ToDescription(Enum value) { var attributes = (DescriptionAttribute[]) value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : value.ToString(); }
/// <summary> /// 获取枚举的Description属性 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string Description(this System.Enum value) { if (value == null) { throw new ArgumentNullException("value"); } string description = value.ToString(); FieldInfo fieldInfo = value.GetType().GetField(description); if (fieldInfo == null) { return(""); } DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; } else { description = value.ToString();//如果没有设置Description返回英文 } return(description); }
/// <summary> /// Gets Enum Value's Description Attribute /// </summary> /// <param name="value">The value you want the description attribute for</param> /// <returns>The description, if any, else it's .ToString()</returns> public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); }
/// <summary> /// Returns either the StringValue attribute from the enumeration, or the string equivalent it /// </summary> public static string GetEnumStringValue(System.Enum value) { FieldInfo fi; StringValueAttribute[] attributes; try { fi = value.GetType().GetField(value.ToString()); attributes = (StringValueAttribute[])fi.GetCustomAttributes(typeof(StringValueAttribute), false); if (attributes != null && attributes.Length > 0) { return(attributes[0].Value); } else { return(value.ToString()); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); return(string.Empty); } }
private static string GetEnumDescription(Enum value) { if (value == null) return null; var type = value.GetType().GetTypeInfo(); var res = value.ToString(); if (type.CustomAttributes.Any(t => t.AttributeType == typeof(FlagsAttribute))) { var members = type.AsType().GetMembers(); var conversion = members.SelectMany(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).Select(a => new KeyValuePair<string, string>(t.Name, ((DescriptionAttribute)a).Description))).ToDictionary(t => t.Key, t => t.Value); res = string.Join(",", res.Split(',').Select(t => t.Trim()).Select(t => conversion.ContainsKey(t) ? conversion[t] : t)); } else { var memInfo = type.AsType().GetMember(value.ToString()); if (memInfo.Length > 0) { var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false).ToList(); if (attrs.Any()) res = ((DescriptionAttribute)attrs.First()).Description; } } return res; }
/// <summary> /// 获取枚举描述信息 /// </summary> /// <param name="en"></param> /// <returns></returns> public static string GetEnumText(this System.Enum en) { string enString = string.Empty; if (null == en) { return(enString); } string key = en.ToString() + "__KEY__"; if (_dictionaryCache.ContainsKey(key)) { enString = _dictionaryCache[key]; } else { string tableName = en.ToString(); var fieldInfo = en.GetType().GetField(tableName); var attributes = (TextAttribute[])fieldInfo.GetCustomAttributes(typeof(TextAttribute), false); if (attributes.Length > 0) { enString = attributes[0].Value; if (!_dictionaryCache.ContainsKey(key)) { _dictionaryCache.Add(key, enString); } else { _dictionaryCache[key] = enString; } } } return(enString); }
/// <summary> /// 获取枚举Description /// </summary> /// <param name="target">枚举参数</param> /// <returns>枚举的Description属性</returns> public static string fetchEnumDescription(this System.Enum target) { FieldInfo fieldInfo = target.GetType().GetField(target.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return((attributes.Length > 0) ? attributes[0].Description : target.ToString()); }
public static string Descricao(this System.Enum @enum) { if (@enum == null) { return(null); } var enumItem = @enum.GetType() .GetMember(@enum.ToString()) .FirstOrDefault(); if (enumItem == null) { return(string.Empty); } var attribute = enumItem.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attribute == null || attribute.Length == 0) { return(@enum.ToString()); } return((attribute[0] as DescriptionAttribute).Description); }
public static string FormatErrorString(this System.Enum enumeration, CultureInfo culture, params object[] information) { Type enumType = enumeration.GetType(); ResourceInfo info = ResourceManagerCache.ResourceInfoForType(enumType); if (culture == null) { culture = info.CultureOverride ?? GlobalCultureOverride; } string formatName = enumeration.ToString(); ResourceManager rm = info.ResourceManager; string errorFormat = rm.GetString(string.Concat(enumType.Name, ".", formatName), culture) ?? rm.GetString(formatName, culture); if (errorFormat == null) { throw new NotSupportedException(ErrorCode.MissingStringDefinition.FormatErrorString(enumType.Name, formatName)); } // TODO: Add prefix information for string... string finalFormat = rm.GetString("_ErrorMessageFormat", culture) ?? "{2}({1}): {0}"; return(string.Format(CultureInfo.InvariantCulture, finalFormat, string.Format(culture, errorFormat, information), // {0} is the formatted error message ((IConvertible)enumeration).ToInt32(CultureInfo.CurrentCulture), // {1} is the number assigned to the enumeration enumType.Namespace, // {2} is the namespace of the enumeration type enumType.Name, // {3} is the name of the enumeration type enumeration.ToString())); // {4} is the name of the enumeration field }
public static string ToDescription(this System.Enum en) { if (en == null) { return(string.Empty); } Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes( typeof(DescriptionAttribute), false); if (attrs != null && attrs.Length > 0) { return(((DescriptionAttribute)attrs[0]).Description); } } return(en.ToString()); }
public ValidationError GetError(System.Enum @enum) { var errorMessage = _resourceManager.GetString(@enum.ToString()); var err = new ValidationError(@enum.ToString(), errorMessage); return(err); }
internal static String GetEnumDescription(Enum i) { FieldInfo fi = (i).GetType().GetField(i.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : i.ToString(); }
public BusinessException(Enum error, Exception innerException = null) : this((int)(object)error, error.GetType().FullName, error.ToString("G"), error.ToString("G"), innerException) { }
/// <summary> /// Gets an optional parameter. /// </summary> /// <returns>The optional.</returns> /// <param name="i">The index.</param> public TValue GetOptional(System.Enum i) { if (d.ContainsKey(i.ToString())) { return(d[i.ToString()]); } return(default(TValue)); }
/// <summary> /// 获取枚举中Description /// </summary> /// <param name="enumname"></param> /// <returns></returns> public static string GetDescription(this System.Enum enumname) { var descriotin = string.Empty; var infofield = enumname.GetType().GetField(enumname.ToString()); var attributes = infofield != null ? (DescriptionAttribute[])infofield.GetCustomAttributes(typeof(DescriptionAttribute), false) : null; return(attributes != null ? attributes[0].Description : enumname.ToString()); }
/// <summary> /// Retrieves the message format from the DefaultMessage attribute decorating the /// message code /// </summary> /// <param name="code"></param> /// <returns></returns> protected string GetMessageFormat(Enum code) { System.Reflection.FieldInfo fi = code.GetType().GetField(code.ToString()); var attributes = (DefaultMessageAttribute[])fi.GetCustomAttributes(typeof(DefaultMessageAttribute), false); return (attributes.Length > 0) ? attributes[0].Message : code.GetType().DeclaringType.FullName + "." + code.ToString(); }
public static string GetDescriptionFromEnumValue(Enum value) { var attribute = value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .SingleOrDefault() as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; }
/// <summary> /// 获取Enum的display name,没有的送货返回枚举本身名字 /// </summary> /// <param name="enumValue"></param> /// <returns></returns> public static string GetDisplayName(this System.Enum enumValue) { return(((DisplayAttribute)(enumValue.GetType() .GetMember(enumValue.ToString()) .First()? .GetCustomAttributes(typeof(DisplayAttribute), false)[0]) ).Name ?? enumValue.ToString()); }
/// <summary> /// Enum値の表示名を取得する /// </summary> /// <param name="enumValue">Enum値</param> /// <returns>表示名</returns> /// <remarks>EnumNamesにリソースない時、値の文字列を返す</remarks> /// <example> /// Enum値の表示名を取得: /// <code> /// string name = Definition.ItemKind.PICTURE.GetDisplayName(); /// </code> /// </example> public static string GetDisplayName(this System.Enum enumValue) { string typeName = enumValue.GetType().Name + "_" + enumValue.ToString(); string displayName = EnumNames.ResourceManager.GetString(typeName); //EnumNamesにリソースない時、値の文字列を返す return(string.IsNullOrEmpty(displayName) ? enumValue.ToString() : displayName); }
public static string GetDescription(Enum cur) { var fi = cur.GetType().GetField(cur.ToString()); var da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); return da != null ? da.Description : cur.ToString(); }
public static string GetDisplayName(this System.Enum val) { return(val.GetType() .GetMember(val.ToString()) .FirstOrDefault() ?.GetCustomAttribute <DisplayAttribute>(false) ?.Name ?? val.ToString()); }
/// <summary> /// /// </summary> /// <param name="enumValue"></param> /// <returns>Description value of given enum value.</returns> /// <exception cref="ArgumentNullException">Thrown when enumValue is null.</exception> public static string GetEnumDescription(Enum enumValue) { if (enumValue == null) throw new ArgumentNullException("enumValue"); var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : enumValue.ToString(); }
public static string GetDescriptionFromEnumValue(System.Enum value) { DescriptionAttribute attribute = value.GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .SingleOrDefault() as DescriptionAttribute; return(attribute == null?value.ToString() : attribute.Description); }
private static string GetDescription(Enum value) { var info = value.GetType().GetField(value.ToString()); var attribute = info.GetCustomAttribute<DescriptionAttribute>(); if (attribute != null) return attribute.Description; else return value.ToString(); }
public static string PegarDescricaoDoEnum(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; return value.ToString(); }
public static string DisplayEnumValue(Enum val) { var t = val.GetType(); var f = t.GetField(val.ToString()); if (f == null) return val.ToString().Replace("_", " "); var a = f.GetCustomAttributes(typeof(ChoiceAttribute), true).OfType<ChoiceAttribute>().FirstOrDefault(); if (a == null) return val.ToString().Replace("_", " "); return a.DisplayName; }
public static string GetDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if ((attributes != null) && (attributes.Length > 0)) return attributes[0].Description; else return value.ToString(); }
/// <summary> /// 从枚举中获取Description /// </summary> /// <param name="targetEnum">需要获取枚举描述的枚举</param> /// <returns>描述内容</returns> public static string GetDescription(this System.Enum targetEnum) { string description; var fieldInfo = targetEnum.GetType().GetField(targetEnum.ToString()); var attr = fieldInfo.GetDescriptAttr(); description = attr != null && attr.Length > 0 ? attr[0].Description : targetEnum.ToString(); return(description); }
public static string GetEnumDescription(System.Enum value) { // source http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c var fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return(attributes.Length > 0 ? attributes[0].Description : value.ToString()); }
public static string GetDescription(this System.Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return(attribute == null?value.ToString() : attribute.Description); }
/// <summary> /// Gets Enum Value's Description Attribute /// </summary> /// <param name="value">The value you want the description attribute for</param> /// <returns>The description, if any, else it's .ToString()</returns> public static string GetEnumDescription(Enum value) { if (value == null) throw new ArgumentNullException("value"); FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[]) fi.GetCustomAttributes( typeof (DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); }
public static string Field(this System.Enum en) { var attributes = en.GetType().GetMember(en.ToString())[0].GetCustomAttributes(typeof(Value), false); if (attributes != null && attributes.Any()) { return(((Value)attributes[0]).CompareValue); } return(en.ToString()); }
public static string GetValue(Enum value) { var fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } return value.ToString(); }
public static string GetDescriptionFromEnumValue(System.Enum value) { DescriptionAttribute descriptionAttribute = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).SingleOrDefault <object>() as DescriptionAttribute; if (descriptionAttribute != null) { return(descriptionAttribute.Description); } return(value.ToString()); }
private static string _GetEnumDesc(Enum enumSubitem) { var fieldinfo = enumSubitem.GetType().GetField(enumSubitem.ToString()); var objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (objs.Length == 0) return enumSubitem.ToString(); var da = (DescriptionAttribute)objs[0]; return da.Description; }
public static string GetEnumDescription(Enum value) { FieldInfo info = value.GetType().GetField(value.ToString()); DescriptionAttribute[] enumAttributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false); if (enumAttributes.Length > 0) { return enumAttributes[0].Description; } return value.ToString(); }
private static String GetEnumDescription(Enum e) { FieldInfo EnumInfo = e.GetType().GetField(e.ToString()); DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo. GetCustomAttributes(typeof(DescriptionAttribute), false); if (EnumAttributes.Length > 0) { return EnumAttributes[0].Description; } return e.ToString(); }
/// <summary> /// Get description that bound to a enum item /// </summary> /// <param name="value">Enum item</param> /// <returns> /// If the specified enum item has no description bound, its name will be returned. /// </returns> public static String GetDescription(Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } }
private string GetEnumDescription(Enum enumObj) { FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString()); object[] attribArray = fieldInfo.GetCustomAttributes(false); if (attribArray.Length == 0) return enumObj.ToString(); DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute; return attrib?.Description; }
public static string GetDescription(Enum enumValue) { var fi = enumValue.GetType().GetField(enumValue.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) return attributes[0].Description; return enumValue.ToString(); }
/// <summary> /// Gets the value of the <see cref="DescriptionAttribute"/> from the specified enum value. /// </summary> /// <param name="value">the enum value to check.</param> /// <returns>The description value of the specified enumeration value.</returns> public static string GetEnumDescription(Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (DescriptionAttribute), false); if (attributes.Length > 0) return attributes[0].Description; return value.ToString(); }
public static string GetDescription(this System.Enum e) { var fi = e.GetType().GetField(e.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return(attributes[0].Description); } return(e.ToString()); }
internal static string GetDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); Description[] attributes = (Description[])fi.GetCustomAttributes(typeof(Description), false); if (attributes.Length > 0) { return attributes[0].Text; } return value.ToString(); }
internal static string GetDescription(Enum e) { var type = e.GetType(); var info = type.GetMember(e.ToString()).Single(); var atts = info.GetCustomAttributes().OfType<DescriptionAttribute>().ToList(); if (atts.Any()) { return atts.First().Description; } return e.ToString(); }
public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attr = fi.GetCustomAttribute<DisplayAttribute>(false); if (attr != null) return attr.GetName(); else return value.ToString(); }
public static string GetEnumDescription(this System.Enum value) { var fi = value.GetType().GetField(value.ToString()); var attributes = fi.GetCustomAttributes(typeof(EnumMemberAttribute), false); if (attributes.Length > 0) { return(((EnumMemberAttribute)attributes[0]).Value); } return(value.ToString()); }
//---------------------------------------------------------------------- #region --- Methods --- //---------------------------------------------------------------------- /// <summary> /// Gets the api url field name for the specified value. /// </summary> /// <param name="value">The value to get the api url field name for.</param> /// <returns> /// The api url field name. /// </returns> public static string GetApiUrlFieldName(Enum value) { var result = value.ToString(); var fieldInfo = value.GetType().GetField(value.ToString()); var attributes = (ApiUrlField[])fieldInfo.GetCustomAttributes(typeof(ApiUrlField), false); if (attributes.Length > 0) { result = attributes[0].Name; } return result; }
private static string GetEnumValueName(System.Enum value) { var field = value.GetType().GetField(value.ToString()); var attribute = field.GetCustomAttribute <EnumMemberAttribute>(); if (attribute == null) { return(value.ToString()); } return(attribute.Value); }
// Enums should use value from SwaggerEnumValueAttribute if it exists. protected override object SerializeEnum(Enum @enum) { var member = @enum.GetType().GetMember(@enum.ToString()).Single(); var attribute = member.GetCustomAttribute<SwaggerEnumValueAttribute>(); if (attribute != null && !string.IsNullOrEmpty(attribute.Value)) { return attribute.Value; } return @enum.ToString(); }
/// <summary> /// 获得枚举值的Description特性的值,一般是消息的搜索码 /// </summary> /// <param name="value">要查找特性的枚举值</param> /// <returns>返回查找到的Description特性的值,如果没有,就返回.ToString()</returns> public static string GetEnumDescription(System.Enum value) { var fi = value.GetType().GetField(value.ToString()); if (fi == null) { return(value.ToString()); } var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return((attributes.Length > 0) ? attributes[0].Description : value.ToString()); }
public static string StringValueOf(System.Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return(attributes[0].Description); } return(value.ToString()); }
public static string GetDescription(Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); if (attrs != null && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return en.ToString(); }