/// <summary> /// Sets the specified delegate object for specified method. /// </summary> /// <param name="method">The method to be created.</param> /// <param name="delegate">The delegate which refers the generated method.</param> public void SetDelegate(CollectionSerializerMethod method, Delegate @delegate) { switch (method) { case CollectionSerializerMethod.AddItem: { this._addItem = @delegate; break; } case CollectionSerializerMethod.CreateInstance: { this._createInstance = @delegate; break; } case CollectionSerializerMethod.RestoreSchema: { // Note: RestoreSchema is not useless in Expression serializer, so nothing to do here. break; } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
protected override void EmitMethodPrologue(TContext context, CollectionSerializerMethod method, MethodInfo declaration) { switch (method) { case CollectionSerializerMethod.AddItem: { context.IL = context.Emitter.GetAddItemMethodILGenerator(declaration); break; } case CollectionSerializerMethod.CreateInstance: { context.IL = context.Emitter.GetCreateInstanceMethodILGenerator(declaration); break; } case CollectionSerializerMethod.RestoreSchema: { context.IL = context.Emitter.GetRestoreSchemaMethodILGenerator(); break; } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
/// <summary> /// Creates the type of the delegate. /// </summary> /// <typeparam name="TObject">The type of serialization target.</typeparam> /// <param name="method">The method to be created.</param> /// <param name="serializerType">The type of callback serializer.</param> /// <param name="traits">The collection traits of the type.</param> /// <returns> /// The <see cref="Type"/> of delegate which can refer to the generating method. /// This value will not be <c>null</c>. /// </returns> /// <exception cref="InvalidOperationException"> /// <paramref name="method"/> is unknown. /// </exception> public static Type CreateDelegateType <TObject>(CollectionSerializerMethod method, Type serializerType, CollectionTraits traits) { switch (method) { case CollectionSerializerMethod.AddItem: { if (traits.DetailedCollectionType == CollectionDetailedKind.GenericDictionary #if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) || traits.DetailedCollectionType == CollectionDetailedKind.GenericReadOnlyDictionary #endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) ) { return (typeof(Action <, , , ,>).MakeGenericType( serializerType, typeof(SerializationContext), typeof(TObject), traits.ElementType.GetGenericArguments()[0], traits.ElementType.GetGenericArguments()[1] )); } else { return (typeof(Action <, , ,>).MakeGenericType( serializerType, typeof(SerializationContext), typeof(TObject), traits.ElementType )); } } case CollectionSerializerMethod.CreateInstance: { return (typeof(Func <, , ,>).MakeGenericType( serializerType, typeof(SerializationContext), typeof(int), typeof(TObject) )); } case CollectionSerializerMethod.RestoreSchema: { return (typeof(Func <>).MakeGenericType( typeof(PolymorphismSchema) )); } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
protected override void EmitMethodEpilogue(ExpressionTreeContext context, CollectionSerializerMethod method, ExpressionConstruct construct) { if (construct == null) { return; } context.SetDelegate(method, this.EmitMethodEpilogue(context, ExpressionTreeContext.CreateDelegateType <TObject>(method, SerializerClass, CollectionTraitsOfThis), method, construct)); }
/// <summary> /// Gets the <see cref="ParameterExpression"/>s for specified method. /// </summary> /// <param name="method">The method to be created.</param> /// <param name="traits">The traits of the collection.</param> /// <returns> /// The <see cref="ParameterExpression"/>s for specified method. /// This value will not be <c>null</c>. /// </returns> private IEnumerable <ParameterExpression> GetParameters(CollectionSerializerMethod method, CollectionTraits traits) { switch (method) { case CollectionSerializerMethod.AddItem: { yield return(this.This.Expression as ParameterExpression); yield return(this._context.Expression as ParameterExpression); yield return(this.CollectionToBeAdded.Expression as ParameterExpression); if (traits.DetailedCollectionType == CollectionDetailedKind.GenericDictionary #if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE) || traits.DetailedCollectionType == CollectionDetailedKind.GenericReadOnlyDictionary #endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE ) ) { yield return(this.KeyToAdd.Expression as ParameterExpression); yield return(this.ValueToAdd.Expression as ParameterExpression); } else { yield return(this.ItemToAdd.Expression as ParameterExpression); } break; } case CollectionSerializerMethod.CreateInstance: { yield return(this.This.Expression as ParameterExpression); yield return(this._context.Expression as ParameterExpression); yield return(this.InitialCapacity.Expression as ParameterExpression); break; } case CollectionSerializerMethod.RestoreSchema: { break; } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
/// <summary> /// Creates the type of the delegate. /// </summary> /// <typeparam name="TObject">The type of serialization target.</typeparam> /// <param name="method">The method to be created.</param> /// <param name="serializerType">The type of callback serializer.</param> /// <param name="traits">The collection traits of the type.</param> /// <returns> /// The <see cref="Type"/> of delegate which can refer to the generating method. /// This value will not be <c>null</c>. /// </returns> /// <exception cref="InvalidOperationException"> /// <paramref name="method"/> is unknown. /// </exception> public static Type CreateDelegateType <TObject>(CollectionSerializerMethod method, Type serializerType, CollectionTraits traits) { switch (method) { case CollectionSerializerMethod.AddItem: { return (typeof(Action <, , ,>).MakeGenericType( serializerType, typeof(SerializationContext), typeof(TObject), traits.ElementType )); } case CollectionSerializerMethod.CreateInstance: { return (typeof(Func <, , ,>).MakeGenericType( serializerType, typeof(SerializationContext), typeof(int), typeof(TObject) )); } case CollectionSerializerMethod.RestoreSchema: { return (typeof(Func <>).MakeGenericType( typeof(PolymorphismSchema) )); } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
/// <summary> /// Gets the <see cref="ParameterExpression"/>s for specified method. /// </summary> /// <param name="method">The method to be created.</param> /// <returns> /// The <see cref="ParameterExpression"/>s for specified method. /// This value will not be <c>null</c>. /// </returns> private IEnumerable <ParameterExpression> GetParameters(CollectionSerializerMethod method) { switch (method) { case CollectionSerializerMethod.AddItem: { yield return(this.This.Expression as ParameterExpression); yield return(this._context.Expression as ParameterExpression); yield return(this.CollectionToBeAdded.Expression as ParameterExpression); yield return(this.ItemToAdd.Expression as ParameterExpression); break; } case CollectionSerializerMethod.CreateInstance: { yield return(this.This.Expression as ParameterExpression); yield return(this._context.Expression as ParameterExpression); yield return(this.InitialCapacity.Expression as ParameterExpression); break; } case CollectionSerializerMethod.RestoreSchema: { break; } default: { throw new ArgumentOutOfRangeException("method", method.ToString()); } } }
protected override void EmitMethodPrologue(ExpressionTreeContext context, CollectionSerializerMethod method, MethodInfo declaration) { context.Reset(typeof(TObject), BaseClass); context.SetCurrentMethod(method, CollectionTraitsOfThis); }
public void SetCurrentMethod(CollectionSerializerMethod method) { this._currentParamters = this.GetParameters(method).ToArray(); }
protected override void EmitMethodEpilogue(TContext context, CollectionSerializerMethod method, TConstruct construct) { Contract.Requires(context != null); Contract.Requires(Enum.IsDefined(typeof(CollectionSerializerMethod), method)); Contract.Requires(construct != null); }
protected override void EmitMethodPrologue(TContext context, CollectionSerializerMethod method, MethodInfo declaration) { Contract.Requires(context != null); Contract.Requires(Enum.IsDefined(typeof(CollectionSerializerMethod), method)); Contract.Requires(declaration != null); }
protected override void EmitMethodEpilogue(TContext context, CollectionSerializerMethod method, ILConstruct construct) { EmitMethodEpilogue(context, construct); }