-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldExtensions.cs
More file actions
32 lines (29 loc) · 799 Bytes
/
Copy pathFieldExtensions.cs
File metadata and controls
32 lines (29 loc) · 799 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace IPAPIClient
{
static class FieldExtensions
{
public static string Map(this Field field)
{
return MapEnum<Field>.ByAttribute<EnumMemberAttribute>.ToAttributeValue(field, x => x.Value);
}
static class MapEnum<TEnum> where TEnum : Enum
{
public static class ByAttribute<TAttr> where TAttr : Attribute
{
public static TResult ToAttributeValue<TResult>(TEnum e, Func<TAttr, TResult> valueSelector)
{
var f = e.GetType().GetField(e.ToString());
var a = f.GetCustomAttributes(typeof(TAttr), true).FirstOrDefault() as TAttr;
if (a is null)
throw new Exception("Oops");
return valueSelector(a);
}
}
}
}
}