Spotify.NET/SpotifyAPI.Web/Util/StringAttribute.cs

42 lines
1.0 KiB
C#
Raw Normal View History

using System;
2024-02-10 11:15:58 +00:00
#if !NETSTANDARD2_0
using System.Diagnostics.CodeAnalysis;
2024-02-10 11:02:50 +00:00
#endif
2022-11-27 12:29:35 +00:00
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; }
2023-11-03 22:23:54 +00:00
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
2020-11-13 13:43:00 +00:00
public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result)
2023-11-03 22:23:54 +00:00
#else
public static bool GetValue(Type enumType, Enum enumValue, out string? result)
#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;
}
}
}