Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Resolvers
|
||||
{
|
||||
public sealed class DynamicEnumAsStringResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly IFormatterResolver Instance = new DynamicEnumAsStringResolver();
|
||||
|
||||
DynamicEnumAsStringResolver()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.formatter;
|
||||
}
|
||||
|
||||
static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
var 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Reference in a new issue