Skip to main content
added 83 characters in body
Source Link
Amir Popovich
  • 30.1k
  • 9
  • 58
  • 102

I guess your looking for something like this (changed the implementation a bit):

public enum Test
{
    [Description("This")]
    A,
    B,
    C,
    D
}

private IEnumerable<string> GetEnumDescription<T>()
{
    var type = typeof(T);

    if (!type.IsEnum) throw new ArgumentException("Only Enum types allowed");

    foreach (var value in Enum.GetValues(typeof(T)type).Cast<T>Cast<Enum>())
    {
        yield return getEnumDescription(value);
    }
}

public string getEnumDescription<T>getEnumDescription(TEnum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

And the call will look like:

var desc = GetEnumDescription<Test>(); // "This", "B", "C", "D"

I guess your looking for something like this (changed the implementation a bit):

public enum Test
{
    [Description("This")]
    A,
    B,
    C,
    D
}

private IEnumerable<string> GetEnumDescription<T>()
{
    foreach (var value in Enum.GetValues(typeof(T)).Cast<T>())
    {
        yield return getEnumDescription(value);
    }
}

public string getEnumDescription<T>(T value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

And the call will look like:

var desc = GetEnumDescription<Test>(); // "This", "B", "C", "D"

I guess your looking for something like this (changed the implementation a bit):

public enum Test
{
    [Description("This")]
    A,
    B,
    C,
    D
}

private IEnumerable<string> GetEnumDescription<T>()
{
    var type = typeof(T);

    if (!type.IsEnum) throw new ArgumentException("Only Enum types allowed");

    foreach (var value in Enum.GetValues(type).Cast<Enum>())
    {
        yield return getEnumDescription(value);
    }
}

public string getEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

And the call will look like:

var desc = GetEnumDescription<Test>(); // "This", "B", "C", "D"
Source Link
Amir Popovich
  • 30.1k
  • 9
  • 58
  • 102

I guess your looking for something like this (changed the implementation a bit):

public enum Test
{
    [Description("This")]
    A,
    B,
    C,
    D
}

private IEnumerable<string> GetEnumDescription<T>()
{
    foreach (var value in Enum.GetValues(typeof(T)).Cast<T>())
    {
        yield return getEnumDescription(value);
    }
}

public string getEnumDescription<T>(T value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

And the call will look like:

var desc = GetEnumDescription<Test>(); // "This", "B", "C", "D"