mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 14:46:26 +00:00
36 lines
858 B
C#
36 lines
858 B
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace SpotifyAPI.Web
|
|
{
|
|
[AttributeUsage(AttributeTargets.Field)]
|
|
public sealed class StringAttribute : Attribute
|
|
{
|
|
public StringAttribute(string value)
|
|
{
|
|
Value = value;
|
|
}
|
|
|
|
public string Value { get; }
|
|
|
|
public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|