33 lines
952 B
C#
33 lines
952 B
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace EnVisage.Code
|
|
{
|
|
public class DisplayValueAttribute : Attribute
|
|
{
|
|
public string DisplayValue { get; set; }
|
|
|
|
public DisplayValueAttribute(string displayValue)
|
|
{
|
|
DisplayValue = displayValue;
|
|
}
|
|
}
|
|
|
|
public static class DisplayValueExtensions
|
|
{
|
|
public static string ToDisplayValue(this Enum value)
|
|
{
|
|
if (value == null)
|
|
return string.Empty;
|
|
|
|
var members = value.GetType().GetMember(value.ToString());
|
|
var attr = members.Length > 0
|
|
? members[0].GetCustomAttributes(typeof (DisplayValueAttribute), true)
|
|
.Cast<DisplayValueAttribute>()
|
|
.FirstOrDefault()
|
|
: null;
|
|
|
|
return attr == null ? value.ToString() : attr.DisplayValue;
|
|
}
|
|
}
|
|
} |