Пример #1
0
 /// <summary>Indicates whether the current object is equal to another object of type <see cref="System.String"/>.
 /// </summary>
 /// <param name="other">An object to compare with this object.</param>
 /// <returns><c>true</c> if the current object is equal to the other parameter; otherwise, <c>false</c>.</returns>
 public bool Equals(string other)
 {
     if (other == null)
     {
         return(false);
     }
     return(StringRepresentation.IDString == IdentifierString.GetIDString(other));
 }
Пример #2
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));
        }
Пример #3
0
        /// <summary>Converts the string representation of an enumeration item to its enumeration item equivalent.</summary>
        /// <param name="stringRepresentation">The <see cref="System.String"/> representation to search for.</param>
        /// <param name="value">The element of the enumeration with respect to <paramref name="stringRepresentation"/> (output).</param>
        /// <param name="enumStringRepresentationUsage">The method how to compute the string representation of the items of the
        /// enumeration represented by <typeparamref name="TEnum"/>.</param>
        /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns>
        /// <remarks>White spaces etc. will be ignored.</remarks>
        /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represent an enumeration.</exception>
        public static bool TryParse(string stringRepresentation, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute)
        {
            if (typeof(TEnum).IsEnum == false)
            {
                throw new ArgumentException(String.Format(ExceptionMessages.ArgumentIsInvalid, "TEnum: " + typeof(TEnum).ToString()));
            }
            if ((stringRepresentation == null) || (stringRepresentation.Length == 0))
            {
                value = default;
                return(false);
            }
            string stringRepresentationID = IdentifierString.GetIDString(stringRepresentation, false);

            if (enumStringRepresentationUsage == EnumStringRepresentationUsage.ToStringMethod)
            {
                return(Enum.TryParse <TEnum>(stringRepresentationID.Replace(EnumString.FlagsEnumSeparatorChar, EnumString.dotNetFlagsEnumSeparatorChar), true, out value));
            }

            ResourceManager resourceManager = null;
            Type            enumerationType = typeof(TEnum);

            if (enumStringRepresentationUsage == EnumStringRepresentationUsage.LanguageStringAttribute)
            {
                if (Attribute.GetCustomAttribute(enumerationType, typeof(LanguageResourceAttribute)) is LanguageResourceAttribute languageResourceFile)
                {
                    resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumerationType.Assembly);
                }
            }

            /* if the enumeration contains the 'Flags' attribute, we assume that the input is given with respect to some
             * comma separated (see 'FlagsEnumSeparatorChar')  list and the return value will be the bitwise OR relation of the corresponding elements. */
            if (enumerationType.GetCustomAttributes(typeof(FlagsAttribute), false).Length == 0) // no [Flags] attribute available
            {
                foreach (TEnum enumValue in Enum.GetValues(enumerationType))                    // linear is no problem because the number of elements is in general very small
                {
                    if (GetFormatString(enumValue, resourceManager) == stringRepresentationID)
                    {
                        value = enumValue;
                        return(true);
                    }
                } // it is not a Flag, but perhaps a bitwise OR combination (which is unusual for a non-flag enumeration)
            }

            /* split the strings into a list of strings, where the 'FlagsEnumSeparatorChar' is take into
             * account, transform each of the sub-strings to some ID-representation, compare to the elements
             * of the given list and build a new string, which will be used for the Parse-Method of System.Enum.
             * Here, we can not apply '|=' to the matched elements, that's the reason to do it this way:
             */
            StringBuilder bString = new StringBuilder();

            foreach (string subString in stringRepresentationID.Split(EnumString.FlagsEnumSeparatorChar))
            {
                foreach (TEnum enumValue in Enum.GetValues(enumerationType))  // linear is no problem because the number of elements is in general very small
                {
                    if (GetFormatString(enumValue, resourceManager) == subString)
                    {
                        if (bString.Length > 0)
                        {
                            bString.Append(EnumString.dotNetFlagsEnumSeparatorChar);
                        }
                        bString.Append(enumValue.ToString());  // transform to the internal string representation
                    }
                }
            }
            return(Enum.TryParse <TEnum>(bString.ToString(), true, out value));
        }
Пример #4
0
 /// <summary>Compares the current object with another object of type <see cref="System.String"/>.
 /// </summary>
 /// <param name="other">An object to compare with this object.</param>
 /// <returns>
 /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:
 /// Less than zero: This object is less than the <paramref name="other"/> parameter.
 /// Zero: This object is equal to <paramref name="other"/>.
 /// Greater than zero: This object is greater than <paramref name="other"/>.
 /// </returns>
 /// <remarks>The <see cref="IdentifierString"/> representation of <paramref name="other"/> is used for the comparisson,
 /// i.e. ignoring white spaces etc.</remarks>
 public int CompareTo(string other)
 {
     return(StringRepresentation.IDString.CompareTo(IdentifierString.GetIDString(other)));
 }