本文整理汇总了C#中System.Enum.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Enum.ToString方法的具体用法?C# Enum.ToString怎么用?C# Enum.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Enum
的用法示例。
在下文中一共展示了Enum.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEnumDescription
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();
}
示例2: ToDescription
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();
}
示例3: GetEnumDescription
/// <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();
}
示例4: GetStringValue
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;
}
示例5: GetEnumDescription
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;
}
示例6: GetEnumDescription
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();
}
示例7: BusinessException
public BusinessException(Enum error, Exception innerException = null)
: this((int)(object)error,
error.GetType().FullName,
error.ToString("G"),
error.ToString("G"),
innerException)
{
}
示例8: GetDescription
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();
}
示例9: GetMessageFormat
/// <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();
}
示例10: GetDescriptionFromEnumValue
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;
}
示例11: PegarDescricaoDoEnum
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();
}
示例12: GetEnumDescription
/// <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();
}
示例13: GetDescription
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();
}
示例14: DisplayEnumValue
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;
}
示例15: GetDescription
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();
}