-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathSmartFlagEnumNameResolver.cs
44 lines (38 loc) · 1.2 KB
/
SmartFlagEnumNameResolver.cs
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
42
43
44
using System;
using global::Utf8Json;
using global::Utf8Json.Formatters;
namespace Ardalis.SmartEnum.Utf8Json;
/// <summary>
///
/// </summary>
public class SmartFlagEnumNameResolver : IJsonFormatterResolver
{
/// <summary>
///
/// </summary>
public static readonly SmartFlagEnumNameResolver Instance = new SmartFlagEnumNameResolver();
private SmartFlagEnumNameResolver()
{
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IJsonFormatter<T> GetFormatter<T>() =>
FormatterCache<T>.Formatter;
private static class FormatterCache<T>
{
public static readonly IJsonFormatter<T> Formatter;
#pragma warning disable S3963 // "static" fields should be initialized inline
static FormatterCache()
{
if (typeof(T).IsSmartFlagEnum(out var genericArguments))
{
var formatterType = typeof(SmartFlagEnumNameFormatter<,>).MakeGenericType(genericArguments);
Formatter = (IJsonFormatter<T>)Activator.CreateInstance(formatterType);
}
}
#pragma warning restore S3963 // "static" fields should be initialized inline
}
}