#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 GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter 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)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter }); return; } else if (!ti.IsEnum) { return; } formatter = (IMessagePackFormatter)(object)new EnumAsStringFormatter(); } } } } #endif