33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
// Copyright (c) 2023- schick Informatik
|
|
// Description: Enum description string conversion helper
|
|
|
|
using System.ComponentModel;
|
|
|
|
namespace brecal.model
|
|
{
|
|
public static class EnumHelper
|
|
{
|
|
public static string Description(this Enum eValue)
|
|
{
|
|
var nAttributes = eValue.GetType().GetField(eValue.ToString())?.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (nAttributes != null)
|
|
{
|
|
if (nAttributes.Any() && nAttributes.First() != null && (nAttributes.First() is DescriptionAttribute da))
|
|
{
|
|
return da.Description;
|
|
}
|
|
}
|
|
return eValue.ToString();
|
|
}
|
|
|
|
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescription(Type t)
|
|
{
|
|
if (!t.IsEnum)
|
|
throw new ArgumentException("t must be an enum");
|
|
return from e in Enum.GetValues(t).Cast<Enum>()
|
|
select new KeyValuePair<string, string>(e.ToString(), e.Description());
|
|
}
|
|
|
|
}
|
|
}
|