Friday, 3 August 2012

Enum description attribute

image
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace EnumPlay
{
enum Position
{
[Description("Last line of defence, always crazy")]
Goalie = 0,

[Description("Big fast and love getting stuck in")]
Defender,

[Description("Play makers and tough tacklers")]
Midfielder,

[Description("Goalscorer for the team")]
Forward
}

public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
}

class Program
{
static void Main(string[] args)
{
OutputValues();
OutputNames();
OutputDescriptions();
}

private static void OutputValues()
{
Console.WriteLine("Values");

var values = Enum.GetValues(typeof(Position));

foreach (int value in values)
{
Console.WriteLine("\t{0}", value);
}

Console.WriteLine();
}

private static void OutputNames()
{
Console.WriteLine("Names");

var names = Enum.GetNames(typeof(Position));

foreach (string name in names)
{
Console.WriteLine("\t{0}", name);
}

Console.WriteLine();
}


private static void OutputDescriptions()
{
Console.WriteLine("Descriptions");

var enums = Enum.GetValues(typeof(Position)).Cast<Position>(); ;

foreach (Position item in enums)
{
var description = item.GetDescription();
Console.WriteLine("\t{0}", description);
}

Console.WriteLine();
}
}
}

Silverlight's missing methods

public static class Enums
{
//EXAMPLE USAGE:
// string[] names = Enums.GetNames<Position>();
public static string[] GetNames<T>()
{
var type = typeof(T);

if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum");

return (from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
where field.IsLiteral
select field.Name).ToArray();
}

// EXAMPLE USAGE:
// Position[] values = Enums.GetValues<Position>();
public static T[] GetValues<T>()
{
var type = typeof(T);

if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum");

return (from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
where field.IsLiteral
select (T)field.GetValue(null)).ToArray();
}

// EXAMPLE USAGE:
// Position position = Position.Goalie;
// string description = position.GetDescription();
public static string GetDescription(this Enum e)
{
FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
DescriptionAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? e.ToString() : attribute.Description;
}

// EXAMPLE USAGE:
// string[] descriptions = Enums.GetDescriptions<Position>();
public static string[] GetDescriptions<T>()
{
var type = typeof(T);

if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum");

var enums = GetValues<T>();
return (from enumeration in enums
select (enumeration as Enum).GetDescription())
.ToArray();
}
}

No comments:

Post a Comment