using FoodsharingSiegen.Contracts.Model;
namespace FoodsharingSiegen.Contracts.Helper
{
///
/// The attribute extensions class (a. beging, 31.05.2022)
///
public static class AttributeExtensions
{
#region Public Method GetCustomValue
///
/// Gets the custom value using the specified enum val (a. beging, 31.05.2022)
///
/// The enum val
/// The string
public static string GetCustomValue(this System.Enum enumVal)
{
var attribute = enumVal.GetAttributeOfType();
return attribute?.Value ?? string.Empty;
}
#endregion
#region Private Method GetAttributeOfType
///
/// Gets the attribute of type using the specified enum val (a. beging, 31.05.2022)
///
/// The
/// The enum val
/// The
private static T? GetAttributeOfType(this System.Enum enumVal) where T : Attribute
{
var type = enumVal.GetType();
var memInfo = type.GetMember(enumVal.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
return attributes.Length > 0 ? (T)attributes[0] : null;
}
#endregion
}
}