Files
FoodsharingOnboarding/FoodsharingSiegen.Contracts/Helper/AttributeExtensions.cs
Andre Beging bf64239625 Refactor enums and update Interaction entity field
Moved enums to a dedicated namespace and updated references across the codebase. Renamed the `Info` field in the `Interaction` entity to `Info1`, including necessary migrations and UI adjustments. These changes improve the organization and consistency of the codebase.
2025-04-01 10:41:09 +02:00

43 lines
1.5 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 System.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 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
}
}