43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using FoodsharingSiegen.Contracts.Model;
|
|
|
|
namespace FoodsharingSiegen.Contracts.Helper
|
|
{
|
|
/// <summary>
|
|
/// The attribute extensions class (a. beging, 31.05.2022)
|
|
/// </summary>
|
|
public static class AttributeExtensions
|
|
{
|
|
#region Public Method GetCustomValue
|
|
|
|
/// <summary>
|
|
/// Gets the custom value using the specified enum val (a. beging, 31.05.2022)
|
|
/// </summary>
|
|
/// <param name="enumVal">The enum val</param>
|
|
/// <returns>The string</returns>
|
|
public static string GetCustomValue(this Enum enumVal)
|
|
{
|
|
var attribute = enumVal.GetAttributeOfType<CustomValueAttribute>();
|
|
return attribute?.Value ?? string.Empty;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method GetAttributeOfType
|
|
|
|
/// <summary>
|
|
/// Gets the attribute of type using the specified enum val (a. beging, 31.05.2022)
|
|
/// </summary>
|
|
/// <typeparam name="T">The </typeparam>
|
|
/// <param name="enumVal">The enum val</param>
|
|
/// <returns>The</returns>
|
|
private static T? GetAttributeOfType<T>(this 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
|
|
}
|
|
} |