|
using System;
using System.ComponentModel;
using System.Text;
namespace Library.Helpers
{
public static class Extensions
{
public static TAttribute GetItemAttribute<TAttribute>(this Enum value)
where TAttribute: Attribute
{
if (value == null)
return default(TAttribute);
var attributes =
(TAttribute[])
value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof (TAttribute), false);
return ((attributes.Length > 0) ? attributes[0] : default(TAttribute));
}
public static string GetItemDescription(this Enum value)
{
return value.GetItemDescription(true);
}
public static string GetItemDescription(this Enum value, bool parseNameIfNoDescription)
{
var descriptionAttribute = value.GetItemAttribute<DescriptionAttribute>();
return ((descriptionAttribute == null)
? value.ToString().SeparateCamelCase()
: descriptionAttribute.Description);
}
public static string SeparateCamelCase(this string name)
{
name.Replace(" ", "");
var parserResult = new StringBuilder();
foreach (var currChar in name)
{
if (!char.IsDigit(currChar)
&&
((currChar.ToString().CompareTo("_") == 0)
|| (currChar.ToString().CompareTo(currChar.ToString().ToUpper()) == 0)))
parserResult.Append(' ');
parserResult.Append(currChar);
}
return parserResult.ToString().Trim();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.