當前位置: 首頁>>代碼示例>>C#>>正文


C# Enum.ToString方法代碼示例

本文整理匯總了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();
        }
開發者ID:Ontropix,項目名稱:whowhat,代碼行數:7,代碼來源:StructHelper.cs

示例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();
 }
開發者ID:jboyflaga,項目名稱:StudInfoSys.AspNetMvc4,代碼行數:7,代碼來源:EnumHelpers.cs

示例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();
 }
開發者ID:aries544,項目名稱:eXpand,代碼行數:12,代碼來源:EnumDescConverter.cs

示例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;
        }
開發者ID:niuxianghui,項目名稱:SkinsModify,代碼行數:35,代碼來源:EnumUtils.cs

示例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;
        }
開發者ID:DotNetDevs,項目名稱:Synology,代碼行數:29,代碼來源:QueryStringParameter.cs

示例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();
		}
開發者ID:xyunbo,項目名稱:jquery.mobile.mvc,代碼行數:8,代碼來源:Misc.cs

示例7: BusinessException

 public BusinessException(Enum error, Exception innerException = null)
     : this((int)(object)error, 
           error.GetType().FullName,
           error.ToString("G"),
           error.ToString("G"),
           innerException)
 {
 }
開發者ID:huisama,項目名稱:meow,代碼行數:8,代碼來源:BusinessException.cs

示例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();
        }
開發者ID:haoljp,項目名稱:ExportBlog,代碼行數:8,代碼來源:App.cs

示例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();
 }
開發者ID:jijo-paulose,項目名稱:bistro-framework,代碼行數:14,代碼來源:DefaultLogger.cs

示例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;
 }
開發者ID:Workker,項目名稱:EHR,代碼行數:8,代碼來源:EnumUtil.cs

示例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();
        }
開發者ID:robertcorreas,項目名稱:WpfExemploEnum,代碼行數:9,代碼來源:Util.cs

示例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();
        }
開發者ID:Klysas,項目名稱:FSANC-V2,代碼行數:15,代碼來源:DescriptionEnum.cs

示例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();
 }
開發者ID:godhand00,項目名稱:Tstaro3,代碼行數:9,代碼來源:EnumDescriptionConverter.cs

示例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;
 }
開發者ID:tiagosarri,項目名稱:NoCaml,代碼行數:9,代碼來源:ListDefinition.cs

示例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();
 }
開發者ID:uwitec,項目名稱:WaiMai,代碼行數:9,代碼來源:EnumHelper.cs


注:本文中的System.Enum.ToString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。