Esempio n. 1
0
        /// <summary>Gets the <see cref="System.String"/> representation of a specific enumeration item.
        /// </summary>
        /// <param name="value">The element of a specific enumeration.</param>
        /// <param name="resourceManager">The resource manager, perhaps <c>null</c> if no resource manager is available for <paramref name="value"/>.</param>
        /// <returns>The <see cref="System.String"/> representation of <paramref name="value"/> taken into account the <paramref name="resourceManager"/>,
        /// i.e. <see cref="LanguageStringAttribute"/> if != <c>null</c>; otherwise <see cref="StringAttribute"/> is used if available;
        /// otherwise the return value of the <c>ToString()</c> method of <paramref name="value"/> will be returned.</returns>
        private static string GetFormatString(TEnum value, ResourceManager resourceManager)
        {
            if (resourceManager != null) // try language dependent string representation
            {
                LanguageStringAttribute languageStringAttribute = EnumAttribute.Create <LanguageStringAttribute>(value);
                if (languageStringAttribute != null)
                {
                    return(IdentifierString.GetIDString(resourceManager.GetString(languageStringAttribute.ResourcePropertyName, Thread.CurrentThread.CurrentUICulture), false));
                }
            }
            StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(value);

            if (stringAttribute != null)
            {
                return(IdentifierString.GetIDString(stringAttribute.StringRepresentation, false));
            }
            return(IdentifierString.GetIDString(value.ToString(), false));
        }
Esempio n. 2
0
        /// <summary>Gets a <see cref="System.String"/> representation that takes into account <see cref="LanguageResourceAttribute"/>,
        /// <see cref="LanguageStringAttribute"/> or <see cref="StringAttribute"/>; if available.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param>
        /// <returns>A <see cref="System.String"/> representation of <paramref name="value"/> with respect to <paramref name="enumStringRepresentationUsage"/>, i.e.
        /// <see cref="LanguageResourceAttribute"/>, <see cref="LanguageStringAttribute"/>, <see cref="StringAttribute"/>
        /// or the <c>ToString()</c> method is used to generate the string representation.</returns>
        public static string ToFormatString(this Enum value, EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute)
        {
            if (enumStringRepresentationUsage == EnumStringRepresentationUsage.ToStringMethod)
            {
                return(value.ToString());
            }
            else if (enumStringRepresentationUsage == EnumStringRepresentationUsage.StringAttribute)
            {
                if (Enum.IsDefined(value.GetType(), value) == true)
                {
                    StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(value);
                    if (stringAttribute != null)
                    {
                        return(stringAttribute.StringRepresentation);
                    }
                    return(value.ToString());
                }
                else // is a flag, i.e. a|b|c etc. which is not a member of the enumeration, but perhaps 'b|c' is in the enumeration!
                {
                    StringBuilder strBuilder = new StringBuilder();

                    foreach (Enum enumValue in GetEnumComponents(value))
                    {
                        if (strBuilder.Length > 0)
                        {
                            strBuilder.Append(EnumString.FlagsEnumSeparatorChar);
                        }
                        StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(enumValue);
                        if (stringAttribute != null)
                        {
                            strBuilder.Append(stringAttribute.StringRepresentation);
                        }
                        else
                        {
                            strBuilder.Append(enumValue.ToString());
                        }
                    }
                    return(strBuilder.ToString());
                }
            }

            // otherwise take into account a language depended string representation, if available:
            Type enumType = value.GetType();
            LanguageResourceAttribute languageResourceFile = Attribute.GetCustomAttribute(enumType, typeof(LanguageResourceAttribute)) as LanguageResourceAttribute;

            if (Enum.IsDefined(value.GetType(), value) == true)
            {
                if (languageResourceFile != null)
                {
                    LanguageStringAttribute languageStringAttribute = EnumAttribute.Create <LanguageStringAttribute>(value);
                    if (languageStringAttribute != null)
                    {
                        ResourceManager resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumType.Assembly);
                        return(resourceManager.GetString(languageStringAttribute.ResourcePropertyName, Thread.CurrentThread.CurrentUICulture));
                    }
                }
                StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(value);
                if (stringAttribute != null)
                {
                    return(stringAttribute.StringRepresentation);
                }
            }
            else  // the enumeration is some [Flag] and only the parts contains a attribute but not the bitwise combination; we use ',' as separator
            {
                StringBuilder strBuilder = new StringBuilder();

                foreach (Enum enumValue in GetEnumComponents(value))
                {
                    if (strBuilder.Length > 0)
                    {
                        strBuilder.Append(EnumString.FlagsEnumSeparatorChar);
                    }

                    if (languageResourceFile != null)
                    {
                        LanguageStringAttribute languageStringAttribute = EnumAttribute.Create <LanguageStringAttribute>(enumValue);
                        if (languageStringAttribute != null)
                        {
                            ResourceManager resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumType.Assembly);
                            strBuilder.Append(resourceManager.GetString(languageStringAttribute.ResourcePropertyName, Thread.CurrentThread.CurrentUICulture));
                            continue;  // to to next enumValue
                        }
                    }
                    StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(enumValue);
                    if (stringAttribute != null)
                    {
                        strBuilder.Append(stringAttribute.StringRepresentation);
                    }
                    else
                    {
                        strBuilder.Append(enumValue.ToString());
                    }
                }
                return(strBuilder.ToString());
            }
            return(value.ToString());
        }