74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
|
// Copyright (c) All contributors. All rights reserved.
|
|||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|||
|
|
|||
|
using System;
|
|||
|
using System.Reflection;
|
|||
|
using MessagePack.Formatters;
|
|||
|
using MessagePack.Internal;
|
|||
|
|
|||
|
namespace MessagePack.Resolvers
|
|||
|
{
|
|||
|
public sealed class DynamicEnumAsStringResolver : IFormatterResolver
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// The singleton instance that can be used.
|
|||
|
/// </summary>
|
|||
|
public static readonly DynamicEnumAsStringResolver Instance;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// A <see cref="MessagePackSerializerOptions"/> instance with this formatter pre-configured.
|
|||
|
/// </summary>
|
|||
|
public static readonly MessagePackSerializerOptions Options;
|
|||
|
|
|||
|
static DynamicEnumAsStringResolver()
|
|||
|
{
|
|||
|
Instance = new DynamicEnumAsStringResolver();
|
|||
|
Options = new MessagePackSerializerOptions(Instance);
|
|||
|
}
|
|||
|
|
|||
|
private DynamicEnumAsStringResolver()
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public IMessagePackFormatter<T> GetFormatter<T>()
|
|||
|
{
|
|||
|
return FormatterCache<T>.Formatter;
|
|||
|
}
|
|||
|
|
|||
|
private static class FormatterCache<T>
|
|||
|
{
|
|||
|
public static readonly IMessagePackFormatter<T> Formatter;
|
|||
|
|
|||
|
static FormatterCache()
|
|||
|
{
|
|||
|
TypeInfo ti = typeof(T).GetTypeInfo();
|
|||
|
|
|||
|
if (ti.IsNullable())
|
|||
|
{
|
|||
|
// build underlying type and use wrapped formatter.
|
|||
|
ti = ti.GenericTypeArguments[0].GetTypeInfo();
|
|||
|
if (!ti.IsEnum)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var innerFormatter = DynamicEnumAsStringResolver.Instance.GetFormatterDynamic(ti.AsType());
|
|||
|
if (innerFormatter == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
|
|||
|
return;
|
|||
|
}
|
|||
|
else if (!ti.IsEnum)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Formatter = (IMessagePackFormatter<T>)(object)new EnumAsStringFormatter<T>();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|