static FormatterCache() { var ti = typeof(T); if (ti.IsNullable()) { #if NET40 ti = ti.GenericTypeArguments()[0]; #else ti = ti.GenericTypeArguments[0]; #endif var innerFormatter = DynamicUnionResolver.Instance.GetFormatterDynamic(ti); if (innerFormatter == null) { return; } formatter = (IMessagePackFormatter <T>)ActivatorUtils.CreateInstance(typeof(StaticNullableFormatter <>).GetCachedGenericType(ti), new object[] { innerFormatter }); return; } var formatterTypeInfo = BuildType(typeof(T)); if (formatterTypeInfo == null) { return; } formatter = (IMessagePackFormatter <T>)ActivatorUtils.FastCreateInstance(formatterTypeInfo.AsType()); }
static FormatterCache() { var ti = typeof(T); if (ti.IsNullable()) { // build underlying type and use wrapped formatter. ti = ti.GenericTypeArguments[0]; if (!ti.IsEnum) { return; } var innerFormatter = DynamicEnumResolver.Instance.GetFormatterDynamic(ti); if (innerFormatter == null) { return; } formatter = (IMessagePackFormatter <T>)ActivatorUtils.CreateInstance(typeof(StaticNullableFormatter <>).GetCachedGenericType(ti), new object[] { innerFormatter }); return; } else if (!ti.IsEnum) { return; } var formatterTypeInfo = BuildType(typeof(T)); formatter = (IMessagePackFormatter <T>)ActivatorUtils.FastCreateInstance(formatterTypeInfo.AsType()); }
static FormatterCache() { var ti = typeof(T); if (ti.IsNullable()) { // build underlying type and use wrapped formatter. #if NET40 ti = ti.GenericTypeArguments()[0]; #else ti = ti.GenericTypeArguments[0]; #endif if (!ti.IsEnum) { return; } var innerFormatter = DynamicEnumAsStringResolver.Instance.GetFormatterDynamic(ti); if (innerFormatter == null) { return; } formatter = (IMessagePackFormatter <T>)ActivatorUtils.CreateInstance(typeof(StaticNullableFormatter <>).GetCachedGenericType(ti), new object[] { innerFormatter }); return; } else if (!ti.IsEnum) { return; } formatter = (IMessagePackFormatter <T>)(object) new EnumAsStringFormatter <T>(); }
public override ValueSerializer BuildSerializer(Serializer serializer, Type type, CachedReadConcurrentDictionary <Type, ValueSerializer> typeMapping) { var ser = new ObjectSerializer(type); typeMapping.TryAdd(type, ser); var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry)); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; ObjectReader reader = (stream, session) => { var count = stream.ReadInt32(session); var instance = (IDictionary)ActivatorUtils.CreateInstance(type, count); if (preserveObjectReferences) { session.TrackDeserializedObject(instance); } for (var i = 0; i < count; i++) { var entry = (DictionaryEntry)stream.ReadObject(session); instance.Add(entry.Key, entry.Value); } return(instance); }; ObjectWriter writer = (stream, obj, session) => { if (preserveObjectReferences) { session.TrackSerializedObject(obj); } var dict = obj as IDictionary; // ReSharper disable once PossibleNullReferenceException Int32Serializer.WriteValueImpl(stream, dict.Count, session); foreach (DictionaryEntry item in dict) { stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer, serializer.Options.PreserveObjectReferences, session); // elementSerializer.WriteValue(stream,item,session); } }; ser.Initialize(reader, writer); return(ser); }
static object CreateInstance(Type genericType, params Type[] genericTypeArguments) { return(ActivatorUtils.CreateInstance(genericType.GetCachedGenericType(genericTypeArguments))); }