2020-05-03 21:34:03 +01:00
|
|
|
using System;
|
2020-07-13 14:49:23 +01:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-05-03 21:34:03 +01:00
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
2020-05-05 14:30:00 +01:00
|
|
|
[AttributeUsage(AttributeTargets.Field)]
|
2020-12-26 17:28:44 +00:00
|
|
|
public sealed class StringAttribute : Attribute
|
2020-05-03 21:34:03 +01:00
|
|
|
{
|
|
|
|
public StringAttribute(string value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
}
|
|
|
|
|
2020-12-26 17:28:44 +00:00
|
|
|
public string Value { get; }
|
2020-07-13 14:49:23 +01:00
|
|
|
|
|
|
|
#if NETSTANDARD2_0
|
|
|
|
public static bool GetValue(Type enumType, Enum enumValue, out string? result)
|
2020-11-13 13:43:00 +00:00
|
|
|
#else
|
|
|
|
public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result)
|
2020-07-13 14:49:23 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(enumType, nameof(enumType));
|
|
|
|
Ensure.ArgumentNotNull(enumValue, nameof(enumValue));
|
|
|
|
|
|
|
|
if (enumType
|
|
|
|
.GetMember(enumValue.ToString())[0]
|
|
|
|
.GetCustomAttributes(typeof(StringAttribute))
|
|
|
|
.FirstOrDefault() is StringAttribute stringAttr)
|
|
|
|
{
|
|
|
|
result = stringAttr.Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
result = null;
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-03 21:34:03 +01:00
|
|
|
}
|
|
|
|
}
|