AnantLabs / codesmith

Automatically exported from code.google.com/p/codesmith
1 stars 0 forks source link

Make enum description extensions #495

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Make enum description extensions (see edits to cst)

//tsj: addition to provide enum ToDescription extensions
public static class EnumExtensions
{
    public static string ToDescription(this IdentifierType val)
    {
        DescriptionAttribute[] attributes = (DescriptionAttribute[])val
            .GetType()
            .GetField(val.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false);
        return attributes.Length > 0 ? attributes[0].Description : string.Empty;
    }

}

Original issue reported on code.google.com by tyler.je...@gmail.com on 7 Sep 2010 at 10:52

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 9 Sep 2010 at 3:54

GoogleCodeExporter commented 9 years ago
You can do this with a generic extension method.  We don't need to generate one 
for each.

        public static string ToDescription(this Enum value)
        {
            DescriptionAttribute[] attributes = (DescriptionAttribute[])value
                .GetType()
                .GetField(value.ToString())
                .GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : string.Empty;
        }

Original comment by paul.wel...@gmail.com on 10 Sep 2010 at 3:18

GoogleCodeExporter commented 9 years ago
Perfect. Why didn't I think of that?

Original comment by tyler.je...@gmail.com on 10 Sep 2010 at 4:21

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 13 Sep 2010 at 7:08

GoogleCodeExporter commented 9 years ago
My enahanced version of this: Caching description by Culture (language) so the 
reflection will only be used once:

/// <summary>The EnumExtensions class</summary>
/// <created author="laurentiu.macovei" date="Thu, 10 Mar 2011 01:08:46 GMT"/>
public static class EnumExtensions
{

    /// <summary>The EnumStorage class</summary>
    /// <created author="laurentiu.macovei" date="Thu, 10 Mar 2011 01:08:46 GMT"/>
    private class EnumStorage<T>
    {
        public static readonly Dictionary<int, Dictionary<T, string>> Description = new Dictionary<int, Dictionary<T, string>>();
    }
    /// <summary>
    /// Returns the localized value of the DisplayAttribute or DescriptionAttribute or the enum.ToString() 
    /// </summary>
    /// <param name="enum">The enum for which to return </param>
    /// <remarks>
    /// You need to decorate your enum with attributes like these:
    /// [Display(Description = "MyResourceName", ResourceType = typeof(MyResourceTypeName))]
    /// or
    /// [Description("My custom description text - not localizable")]
    /// 
    /// Please NOTE that the value is cached per culture info and enum value for faster subsequent access.
    /// </remarks>
    /// <created author="laurentiu.macovei" date="Thu, 10 Mar 2011 01:08:46 GMT"/>
    [DebuggerStepThrough]
    public static string AsString<T>(this T @enum) //ext method
        where T : struct, IComparable, IFormattable, IConvertible
    {
        string result;
        Dictionary<T, string> dic;
        var cultureHash = Thread.CurrentThread.CurrentUICulture.GetHashCode();
        if (!EnumStorage<T>.Description.TryGetValue(cultureHash, out dic))
        {
            dic = new Dictionary<T, string>();
            EnumStorage<T>.Description[cultureHash] = dic;
        }
        if (dic.TryGetValue(@enum, out result))
            return result;
        if (@enum is int)
            return @enum.ToString();
        var type = @enum.GetType();
        MemberInfo[] memInfo = type.GetMember(@enum.ToString());

        if (memInfo != null && memInfo.Length > 0)
        {

            object[] attrs = memInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
            if (attrs != null && attrs.Length > 0)
                result = ((DisplayAttribute)attrs[0]).GetDescription();
            else
            {
                attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0)
                    result = ((DescriptionAttribute)attrs[0]).Description;

            }
        }
        if (result == null)
            result = @enum.ToString();
        dic[@enum] = result;
        return result;
    }
}

Original comment by alonec...@gmail.com on 15 Mar 2011 at 1:45

GoogleCodeExporter commented 9 years ago
Hello,

Thanks for this update.

-Blake

Original comment by bniemyjski on 22 Mar 2011 at 11:47