广西桂平帝恒管道燃气投资有限公司多角色网上演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

60 lines
1.7 KiB

#if !UNITY_METRO
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