// ReSharper disable UnusedParameter.Local private static MessagePackSerializer TryCreateImmutableCollectionSerializer(SerializationContext context, Type targetType, PolymorphismSchema schema) { #if NETFX_35 || NETFX_40 || SILVERLIGHT // ImmutableCollections does not support above platforms. return(null); #else if (targetType.Namespace != "System.Collections.Immutable") { return(null); } if (!targetType.GetIsGenericType()) { return(null); } var itemSchema = (schema ?? PolymorphismSchema.Default); switch (DetermineImmutableCollectionType(targetType)) { case ImmutableCollectionType.ImmutableArray: case ImmutableCollectionType.ImmutableList: case ImmutableCollectionType.ImmutableHashSet: case ImmutableCollectionType.ImmutableSortedSet: case ImmutableCollectionType.ImmutableQueue: { return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(ImmutableCollectionSerializerFactory <,>).MakeGenericType(targetType, targetType.GetGenericArguments()[0]) ).Create(context, itemSchema)); } case ImmutableCollectionType.ImmutableStack: { return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(ImmutableStackSerializerFactory <,>).MakeGenericType(targetType, targetType.GetGenericArguments()[0]) ).Create(context, itemSchema)); } case ImmutableCollectionType.ImmutableDictionary: case ImmutableCollectionType.ImmutableSortedDictionary: { return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(ImmutableDictionarySerializerFactory <, ,>).MakeGenericType(targetType, targetType.GetGenericArguments()[0], targetType.GetGenericArguments()[1]) ).Create(context, itemSchema)); } default: { #if DEBUG Contract.Assert(false, "Unknown type:" + targetType); #endif // DEBUG // ReSharper disable HeuristicUnreachableCode return(null); // ReSharper restore HeuristicUnreachableCode } } #endif // NETFX_35 || NETFX_40 || SILVERLIGHT }
protected override Func <SerializationContext, MessagePackSerializer <TObject> > CreateEnumSerializerConstructor(ExpressionTreeContext codeGenerationContext) { #if !NETFX_CORE && !SILVERLIGHT if (SerializerDebugging.DumpEnabled) { this._typeBuilder.CreateType(); } #endif // Get at this point to prevent unexpected context change. var packUnderyingValueTo = codeGenerationContext.GetPackUnderyingValueTo(); var unpackFromUnderlyingValue = codeGenerationContext.GetUnpackFromUnderlyingValue(); var targetType = typeof(ExpressionCallbackEnumMessagePackSerializer <>).MakeGenericType(typeof(TObject)); return (context => ReflectionExtensions.CreateInstancePreservingExceptionType <MessagePackSerializer <TObject> >( targetType, context, EnumMessagePackSerializerHelpers.DetermineEnumSerializationMethod( context, typeof(TObject), EnumMemberSerializationMethod.Default ), packUnderyingValueTo, unpackFromUnderlyingValue )); }
private static MessagePackSerializer CreateNullableSerializer(SerializationContext context, Type underlyingType, PolymorphismSchema schema) { return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(NullableInstanceFactory <>).MakeGenericType(underlyingType) ).Create(context, schema)); }
public static MessagePackSerializer <T> CreateReflectionEnumMessagePackSerializer <T>(SerializationContext context) { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <MessagePackSerializer <T> >( typeof(ReflectionEnumMessagePackSerializer <>).MakeGenericType(typeof(T)), context )); #else return(MessagePackSerializer.Wrap <T>(context, new ReflectionEnumMessagePackSerializer(context, typeof(T)))); #endif // !UNITY }
private static IMessagePackSingleObjectSerializer CreateListSerializer(SerializationContext context, Type itemType, PolymorphismSchema schema) { #if DEBUG && !XAMIOS && !XAMDROID && !UNITY_IPHONE && !UNITY_ANDROID if (SerializerDebugging.AvoidsGenericSerializer) { return(null); } #endif // DEBUG && !XAMIOS && !XAMDROID && !UNITY_IPHONE && !UNITY_ANDROID return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(ListInstanceFactory <>).MakeGenericType(itemType) ).Create(context, schema)); }
private static MessagePackSerializer CreateDictionarySerializer(SerializationContext context, Type keyType, Type valueType, PolymorphismSchema schema) { #if DEBUG && !XAMARIN && !UNITY_IPHONE && !UNITY_ANDROID if (SerializerDebugging.AvoidsGenericSerializer) { return(null); } #endif // DEBUG && !XAMARIN && !UNITY_IPHONE && !UNITY_ANDROID return (ReflectionExtensions.CreateInstancePreservingExceptionType <IGenericBuiltInSerializerFactory>( typeof(DictionaryInstanceFactory <,>).MakeGenericType(keyType, valueType) ).Create(context, schema)); }
/// <summary> /// Initializes a new instance of the <see cref="GenericExceptionTester<T>"/> class. /// </summary> public GenericExceptionTester() { try { #if UNITY this._defaultConstructor = () => ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T)); #elif !NETFX_CORE this._defaultConstructor = Expression.Lambda <Func <T> >(Expression.New(typeof(T).GetConstructor(_emptyTypes))).Compile(); #else this._defaultConstructor = Expression.Lambda <Func <T> >(Expression.New(typeof(T).GetTypeInfo().DeclaredConstructors.Single(c => c.GetParameters().Length == 0))).Compile(); #endif // if UNITY elif !NETFX_CORE var message = Expression.Parameter(typeof(string), "message"); var innerException = Expression.Parameter(typeof(Exception), "innerException"); #if !UNITY this._messageConstructor = Expression.Lambda <Func <string, T> >( #if !NETFX_CORE Expression.New(typeof(T).GetConstructor(_messageConstructorParameterTypes), message), #else Expression.New(typeof(T).GetTypeInfo().DeclaredConstructors.Single(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(_messageConstructorParameterTypes)), message), #endif // !NETFX_CORE message ).Compile(); #else this._messageConstructor = msg => ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T), msg); #endif // !UNITY #if !UNITY this._innerExceptionConstructor = Expression.Lambda <Func <string, Exception, T> >( #if !NETFX_CORE Expression.New(typeof(T).GetConstructor(_innerExceptionConstructorParameterTypes), message, innerException), #else Expression.New(typeof(T).GetTypeInfo().DeclaredConstructors.Single(c => c.GetParameters().Select(p => p.ParameterType).SequenceEqual(_innerExceptionConstructorParameterTypes)), message, innerException), #endif // !NETFX_CORE message, innerException ).Compile(); #else this._innerExceptionConstructor = (msg, inner) => ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T), msg, inner); #endif // !UNITY } catch (Exception ex) { this._constructorException = ex; } }
private T CreateTuple(IList <object> unpackedItems) { object currentTuple = null; for (var nest = this._tupleTypes.Count - 1; nest >= 0; nest--) { var items = unpackedItems.Skip(nest * 7).Take(Math.Min(unpackedItems.Count, 7)).ToList(); if (currentTuple != null) { items.Add(currentTuple); } currentTuple = this._tupleConstructors[nest] == null ? ReflectionExtensions.CreateInstancePreservingExceptionType(this._tupleTypes[nest]) : this._tupleConstructors[nest].InvokePreservingExceptionType(items.ToArray()); } return(( T )currentTuple); }
public static object UnpackGenericArraySegmentFrom(Unpacker unpacker, Type elementType, IMessagePackSingleObjectSerializer itemSerializer) { Array array = Array.CreateInstance(elementType, unpacker.ItemsCount); for (int i = 0; i < array.Length; i++) { if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } array.SetValue(itemSerializer.UnpackFrom(unpacker), i); } return (ReflectionExtensions.CreateInstancePreservingExceptionType( typeof(ArraySegment <>).MakeGenericType(elementType), array )); }
public override Func <SerializationContext, EnumSerializationMethod, MessagePackSerializer <T> > CreateConstructor <T>() { Contract.Assert(this._targetType == typeof(T)); var packUnderyingValueTo = this._packUnderyingValueToMethod.CreateDelegate(typeof(Action <SerializationContext, Packer, T>)) as Action <SerializationContext, Packer, T>; var unpackFromUnderlyingValue = this._unpackFromUnderlyingValueMethod.CreateDelegate(typeof(Func <SerializationContext, MessagePackObject, T>)) as Func <SerializationContext, MessagePackObject, T>; var targetType = typeof(CallbackEnumMessagePackSerializer <>).MakeGenericType(typeof(T)); return ((context, method) => ReflectionExtensions.CreateInstancePreservingExceptionType <MessagePackSerializer <T> >( targetType, context, method, packUnderyingValueTo, unpackFromUnderlyingValue )); }
public static MessagePackSerializer Create(SerializationContext context, Type targetType, PolymorphismSchema itemsSchema) { #if DEBUG Contract.Assert(targetType.IsArray, "targetType.IsArray"); #endif // DEBUG // Check the T is SZArray -- Type.GetArrayRank() returns 1 for single dimension, non-zero based arrays, so use (SZArrayType).IsAssinableFrom() instead. if (targetType.GetElementType().MakeArrayType().IsAssignableFrom(targetType)) { return ((GetPrimitiveArraySerializer(context, targetType) #if !UNITY ?? ReflectionExtensions.CreateInstancePreservingExceptionType( typeof(ArraySerializer <>).MakeGenericType(targetType.GetElementType()), context, itemsSchema ) #else ?? new UnityArraySerializer(context, targetType.GetElementType(), itemsSchema) #endif ) as MessagePackSerializer); } else { return (#if !UNITY ReflectionExtensions.CreateInstancePreservingExceptionType( typeof(MultidimensionalArraySerializer <,>).MakeGenericType(targetType, targetType.GetElementType()), context, itemsSchema ) as MessagePackSerializer); #else new UnityMultidimensionalArraySerializer(context, targetType.GetElementType(), itemsSchema); #endif } }
public static Func <SerializationContext, MessagePackSerializer <TObject> > CreateFactory <TObject>(ExpressionTreeContext codeGenerationContext, CollectionTraits traits, PolymorphismSchema schema) { // Get at this point to prevent unexpected context change. var packToCore = codeGenerationContext.GetPackToCore(); var unpackFromCore = codeGenerationContext.GetUnpackFromCore(); var unpackToCore = codeGenerationContext.GetUnpackToCore(); var createInstance = codeGenerationContext.GetCreateInstance(); var addItem = codeGenerationContext.GetAddItem(); switch (traits.DetailedCollectionType) { case CollectionDetailedKind.NonGenericEnumerable: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(NonGenericEnumerableCallbackSerializerFactory <>).MakeGenericType(typeof(TObject)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>); } case CollectionDetailedKind.NonGenericCollection: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(NonGenericCollectionCallbackSerializerFactory <>).MakeGenericType(typeof(TObject)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>); } case CollectionDetailedKind.NonGenericList: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(NonGenericListCallbackSerializerFactory <>).MakeGenericType(typeof(TObject)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } case CollectionDetailedKind.NonGenericDictionary: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(NonGenericDictionaryCallbackSerializerFactory <>).MakeGenericType(typeof(TObject)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } case CollectionDetailedKind.GenericEnumerable: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(EnumerableCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>); } case CollectionDetailedKind.GenericCollection: case CollectionDetailedKind.GenericSet: case CollectionDetailedKind.GenericList: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(CollectionCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } #if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) case CollectionDetailedKind.GenericReadOnlyCollection: case CollectionDetailedKind.GenericReadOnlyList: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(ReadOnlyCollectionCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } #endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) case CollectionDetailedKind.GenericDictionary: { var keyValuePairGenericArguments = traits.ElementType.GetGenericArguments(); var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(DictionaryCallbackSerializerFactory <, ,>).MakeGenericType( typeof(TObject), keyValuePairGenericArguments[0], keyValuePairGenericArguments[1] ) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } #if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) case CollectionDetailedKind.GenericReadOnlyDictionary: { var keyValuePairGenericArguments = traits.ElementType.GetGenericArguments(); var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(ReadOnlyDictionaryCallbackSerializerFactory <, ,>).MakeGenericType( typeof(TObject), keyValuePairGenericArguments[0], keyValuePairGenericArguments[1] ) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>); } #endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) default: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICallbackSerializerFactory>( typeof(CallbackSerializerFactory <>).MakeGenericType( typeof(TObject) ) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return (context => factory.Create(context, packToCore, unpackFromCore, unpackToCore) as MessagePackSerializer <TObject>); } } }
public static IMessagePackSingleObjectSerializer CreateCollectionSerializer <T>( #endif // !UNITY SerializationContext context, Type targetType, CollectionTraits traits, PolymorphismSchema schema ) { switch (traits.DetailedCollectionType) { case CollectionDetailedKind.Array: { return(ArraySerializer.Create <T>(context, schema)); } case CollectionDetailedKind.GenericList: #if !NETFX_35 && !UNITY case CollectionDetailedKind.GenericSet: #endif // !NETFX_35 && !UNITY case CollectionDetailedKind.GenericCollection: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(CollectionSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType) ).Create(context, targetType, schema)); #else new ReflectionCollectionMessagePackSerializer(context, typeof(T), targetType, traits, schema); #endif // !UNITY } case CollectionDetailedKind.GenericEnumerable: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(EnumerableSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType) ).Create(context, targetType, schema)); #else new ReflectionEnumerableMessagePackSerializer(context, typeof(T), targetType, traits, schema); #endif // !Enumerable } case CollectionDetailedKind.GenericDictionary: { var genericArgumentOfKeyValuePair = traits.ElementType.GetGenericArguments(); return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(DictionarySerializerFactory <, ,>).MakeGenericType( typeof(T), genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1] ) ).Create(context, targetType, schema)); #else new ReflectionDictionaryMessagePackSerializer( context, typeof(T), targetType, genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1], traits, schema ); #endif // !UNITY } case CollectionDetailedKind.NonGenericList: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(NonGenericListSerializerFactory <>).MakeGenericType(typeof(T)) ).Create(context, targetType, schema)); #else new ReflectionNonGenericListMessagePackSerializer(context, typeof(T), targetType, schema); #endif // !UNITY } case CollectionDetailedKind.NonGenericCollection: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(NonGenericCollectionSerializerFactory <>).MakeGenericType(typeof(T), traits.ElementType) ).Create(context, targetType, schema)); #else new ReflectionNonGenericCollectionMessagePackSerializer(context, typeof(T), targetType, schema); #endif // !UNITY } case CollectionDetailedKind.NonGenericEnumerable: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(NonGenericEnumerableSerializerFactory <>).MakeGenericType(typeof(T), traits.ElementType) ).Create(context, targetType, schema)); #else new ReflectionNonGenericEnumerableMessagePackSerializer(context, typeof(T), targetType, schema); #endif // !UNITY } case CollectionDetailedKind.NonGenericDictionary: { return (#if !UNITY (MessagePackSerializer <T>) ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>( typeof(NonGenericDictionarySerializerFactory <>).MakeGenericType(typeof(T)) ).Create(context, targetType, schema)); #else new ReflectionNonGenericDictionaryMessagePackSerializer(context, typeof(T), targetType, schema); #endif // !UNITY } default: { return(null); } } }
internal static MessagePackSerializer TryCreateAbstractCollectionSerializer(SerializationContext context, Type abstractType, Type concreteType, PolymorphismSchema schema, CollectionTraits traits) { switch (traits.DetailedCollectionType) { case CollectionDetailedKind.GenericList: #if !NETFX_35 && !UNITY case CollectionDetailedKind.GenericSet: #endif // !NETFX_35 && !UNITY case CollectionDetailedKind.GenericCollection: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(CollectionSerializerFactory <,>).MakeGenericType(abstractType, traits.ElementType) ).Create(context, concreteType, schema)); #else return(new AbstractCollectionMessagePackSerializer(context, abstractType, concreteType, traits, schema)); #endif } #if !NETFX_35 && !UNITY && !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) case CollectionDetailedKind.GenericReadOnlyList: case CollectionDetailedKind.GenericReadOnlyCollection: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(ReadOnlyCollectionSerializerFactory <,>).MakeGenericType(abstractType, traits.ElementType) ).Create(context, concreteType, schema)); #else return(new AbstractCollectionMessagePackSerializer(context, abstractType, concreteType, traits, schema)); #endif } #endif // !NETFX_35 && !UNITY && !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) case CollectionDetailedKind.GenericEnumerable: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(EnumerableSerializerFactory <,>).MakeGenericType(abstractType, traits.ElementType) ).Create(context, concreteType, schema)); #else return(new AbstractEnumerableMessagePackSerializer(context, abstractType, concreteType, traits, schema)); #endif } case CollectionDetailedKind.GenericDictionary: { var genericArgumentOfKeyValuePair = traits.ElementType.GetGenericArguments(); #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(DictionarySerializerFactory <, ,>).MakeGenericType( abstractType, genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1] ) ).Create(context, concreteType, schema)); #else return(new AbstractDictionaryMessagePackSerializer(context, abstractType, concreteType, genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1], traits, schema)); #endif } #if !NETFX_35 && !UNITY && !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) case CollectionDetailedKind.GenericReadOnlyDictionary: { var genericArgumentOfKeyValuePair = traits.ElementType.GetGenericArguments(); #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(ReadOnlyDictionarySerializerFactory <, ,>).MakeGenericType( abstractType, genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1] ) ).Create(context, concreteType, schema)); #else return(new AbstractDictionaryMessagePackSerializer(context, abstractType, concreteType, genericArgumentOfKeyValuePair[0], genericArgumentOfKeyValuePair[1], traits, schema)); #endif } #endif // !NETFX_35 && !UNITY && !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) case CollectionDetailedKind.NonGenericList: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(NonGenericListSerializerFactory <>).MakeGenericType(abstractType) ).Create(context, concreteType, schema)); #else return(new AbstractNonGenericListMessagePackSerializer(context, abstractType, concreteType, schema)); #endif } case CollectionDetailedKind.NonGenericCollection: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(NonGenericCollectionSerializerFactory <>).MakeGenericType(abstractType) ).Create(context, concreteType, schema)); #else return(new AbstractNonGenericCollectionMessagePackSerializer(context, abstractType, concreteType, schema)); #endif } case CollectionDetailedKind.NonGenericEnumerable: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(NonGenericEnumerableSerializerFactory <>).MakeGenericType(abstractType) ).Create(context, concreteType, schema)); #else return(new AbstractNonGenericEnumerableMessagePackSerializer(context, abstractType, concreteType, schema)); #endif } case CollectionDetailedKind.NonGenericDictionary: { #if !UNITY return (ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantSerializerFactory>( typeof(NonGenericDictionarySerializerFactory <>).MakeGenericType(abstractType) ).Create(context, concreteType, schema)); #else return(new AbstractNonGenericDictionaryMessagePackSerializer(context, abstractType, concreteType, schema)); #endif } default: { return(null); } } }
protected internal override T UnpackFromCore(Unpacker unpacker) { object result = this._constructorParameters == null ? ReflectionExtensions.CreateInstancePreservingExceptionType(typeof(T)) : this._constructorParameters.Select(p => p.GetHasDefaultValue() ? p.DefaultValue : p.ParameterType.GetIsValueType() ? ReflectionExtensions.CreateInstancePreservingExceptionType(p.ParameterType) : null ).ToArray(); var unpacked = 0; var asUnpackable = result as IUnpackable; if (asUnpackable != null) { asUnpackable.UnpackFromMessage(unpacker); return(( T )result); } if (unpacker.IsArrayHeader) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { result = this.UnpackMemberValue(result, unpacker, itemsCount, ref unpacked, i, i); } } else { #if DEBUG && !UNITY Contract.Assert(unpacker.IsMapHeader, "unpacker.IsMapHeader"); #endif // DEBUG && !UNITY var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { string name; if (!unpacker.ReadString(out name)) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } if (name == null) { // missing member, drain the value and discard it. if (!unpacker.Read()) { throw SerializationExceptions.NewMissingItem(i); } continue; } int index; if (!this._memberIndexes.TryGetValue(name, out index)) { // key does not exist in the object, skip the associated value if (unpacker.Skip() == null) { throw SerializationExceptions.NewMissingItem(i); } continue; } result = this.UnpackMemberValue(result, unpacker, itemsCount, ref unpacked, index, i); } } if (this._constructorParameters == null) { return(( T )result); } else { return(ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T), result as object[])); } }
public override Func <SerializationContext, PolymorphismSchema, MessagePackSerializer <T> > CreateConstructor <T>() { var traits = typeof(T).GetCollectionTraits(); // Avoids capture of this pointer. var createInstanceMethod = this._createInstanceMethod; var unpackFromMethod = this._unpackFromMethod; var addItemMethod = this._addItemMethod; switch (traits.DetailedCollectionType) { case CollectionDetailedKind.NonGenericEnumerable: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(NonGenericEnumerableCallbackSerializerFactory <>).MakeGenericType(typeof(T)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod, unpackFromMethod, addItemMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.NonGenericCollection: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(NonGenericCollectionCallbackSerializerFactory <>).MakeGenericType(typeof(T)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod, unpackFromMethod, addItemMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.NonGenericList: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(NonGenericListCallbackSerializerFactory <>).MakeGenericType(typeof(T)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.NonGenericDictionary: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(NonGenericDictionaryCallbackSerializerFactory <>).MakeGenericType(typeof(T)) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.GenericEnumerable: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>( typeof(EnumerableCallbackSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod, unpackFromMethod, addItemMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.GenericCollection: #if !NETFX_35 && !UNITY case CollectionDetailedKind.GenericSet: #endif // !NETFX_35 && !UNITY case CollectionDetailedKind.GenericList: { var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(CollectionCallbackSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod) as MessagePackSerializer <T>); } case CollectionDetailedKind.GenericDictionary: { var keyValuePairGenericArguments = traits.ElementType.GetGenericArguments(); var factory = ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>( typeof(DictionaryCallbackSerializerFactory <, ,>).MakeGenericType( typeof(T), keyValuePairGenericArguments[0], keyValuePairGenericArguments[1] ) ); #if DEBUG Contract.Assert(factory != null); #endif // DEBUG return ((context, schema) => factory.Create(context, schema, createInstanceMethod) as MessagePackSerializer <T>); } default: { var packTo = this._packToMethod.CreateDelegate(typeof(Action <SerializationContext, Packer, T>)) as Action <SerializationContext, Packer, T>; var unpackFrom = unpackFromMethod.CreateDelegate(typeof(Func <SerializationContext, Unpacker, T>)) as Func <SerializationContext, Unpacker, T>; var unpackTo = default(Action <SerializationContext, Unpacker, T>); if (this._unpackToMethod != null) { unpackTo = this._unpackToMethod.CreateDelegate(typeof(Action <SerializationContext, Unpacker, T>)) as Action <SerializationContext, Unpacker, T>; } return((context, schema) => new CallbackMessagePackSerializer <T>(context, packTo, unpackFrom, unpackTo)); } } }
} // UnpackAndAddCollectionItem #if FEATURE_TAP protected internal override async Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken) { object result = this._constructorParameters == null ? ReflectionExtensions.CreateInstancePreservingExceptionType(typeof(T)) : this._constructorParameters.Select(p => p.GetHasDefaultValue() ? p.DefaultValue : p.ParameterType.GetIsValueType() ? ReflectionExtensions.CreateInstancePreservingExceptionType(p.ParameterType) : null ).ToArray(); var unpacked = 0; var asAsyncUnpackable = result as IAsyncUnpackable; if (asAsyncUnpackable != null) { await asAsyncUnpackable.UnpackFromMessageAsync(unpacker, cancellationToken).ConfigureAwait(false); return(( T )result); } var asUnpackable = result as IUnpackable; if (asUnpackable != null) { await Task.Run(() => asUnpackable.UnpackFromMessage(unpacker), cancellationToken).ConfigureAwait(false); return(( T )result); } if (unpacker.IsArrayHeader) { var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { result = await this.UnpackMemberValueAsync(result, unpacker, itemsCount, unpacked, i, i, cancellationToken).ConfigureAwait(false); unpacked++; } } else { #if DEBUG Contract.Assert(unpacker.IsMapHeader, "unpacker.IsMapHeader"); #endif // DEBUG var itemsCount = UnpackHelpers.GetItemsCount(unpacker); for (int i = 0; i < itemsCount; i++) { var name = await unpacker.ReadStringAsync(cancellationToken).ConfigureAwait(false); if (!name.Success) { SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker); } if (name.Value == null) { // missing member, drain the value and discard it. if (!unpacker.Read() && i < itemsCount - 1) { SerializationExceptions.ThrowMissingKey(i + 1, unpacker); } continue; } int index; if (!this._memberIndexes.TryGetValue(name.Value, out index)) { // key does not exist in the object, skip the associated value if (unpacker.Skip() == null) { SerializationExceptions.ThrowMissingItem(i, unpacker); } continue; } result = await this.UnpackMemberValueAsync(result, unpacker, itemsCount, unpacked, index, i, cancellationToken).ConfigureAwait(false); unpacked++; } } if (this._constructorParameters == null) { return(( T )result); } else { return(ReflectionExtensions.CreateInstancePreservingExceptionType <T>(typeof(T), result as object[])); } }