using System;
using System.ComponentModel;
using System.Globalization;
namespace BPA.UIControl.Commons
{
///
/// Enum -> Description
///
public class EnumDescriptionConverter : EnumConverter
{
///
/// Initializes a new instance of the class.
///
/// The type.
public EnumDescriptionConverter(Type type)
: base(type)
{
}
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return value?.GetDescription();
}
return string.Empty;
}
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
var enumValues = EnumType.GetEnumValues();
for (int i = 0; i < enumValues.Length; i++)
{
var enumValue = enumValues.GetValue(i);
if (value.Equals(enumValue.GetDescription()))
{
return enumValue;
}
}
}
return base.ConvertFrom(context, culture, value);
}
}
}