forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAttribute.cs
More file actions
41 lines (37 loc) · 1.01 KB
/
StringAttribute.cs
File metadata and controls
41 lines (37 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
#if !NETSTANDARD2_0
using System.Diagnostics.CodeAnalysis;
#endif
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; }
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
public static bool GetValue(Type enumType, Enum enumValue, [NotNullWhen(true)] out string? result)
#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;
}
}
}