using MessagePack.Formatters; using System; using System.Reflection; using System.Linq; // require UNITY_WSA namespace MessagePack.Resolvers { /// /// Get formatter from [MessaegPackFromatter] attribute. /// public sealed class AttributeFormatterResolver : IFormatterResolver { public static IFormatterResolver Instance = new AttributeFormatterResolver(); AttributeFormatterResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { #if UNITY_WSA && !NETFX_CORE var attr = (MessagePackFormatterAttribute)typeof(T).GetCustomAttributes(typeof(MessagePackFormatterAttribute), true).FirstOrDefault(); #else var attr = typeof(T).GetTypeInfo().GetCustomAttribute(); #endif if (attr == null) { return; } if (attr.Arguments == null) { formatter = (IMessagePackFormatter)Activator.CreateInstance(attr.FormatterType); } else { formatter = (IMessagePackFormatter)Activator.CreateInstance(attr.FormatterType, attr.Arguments); } } } } }