/// <summary> /// Regisgters <see cref="MessagePackSerializer{T}"/> of target type usage to the current emitting session. /// </summary> /// <param name="targetType">The type of the member to be serialized/deserialized.</param> /// <param name="enumMemberSerializationMethod">The enum serialization method of the member to be serialized/deserialized.</param> /// <param name="dateTimeConversionMethod">The date time conversion method of the member to be serialized/deserialized.</param> /// <param name="polymorphismSchema">The schema for polymorphism support.</param> /// <param name="schemaRegenerationCodeProvider">The delegate to provide constructs to emit schema regeneration codes.</param> /// <returns><see cref="FieldBuilder"/>.</returns> public FieldBuilder RegisterSerializer( Type targetType, EnumMemberSerializationMethod enumMemberSerializationMethod, DateTimeMemberConversionMethod dateTimeConversionMethod, PolymorphismSchema polymorphismSchema, Func <IEnumerable <ILConstruct> > schemaRegenerationCodeProvider ) { var key = new SerializerFieldKey(targetType, enumMemberSerializationMethod, dateTimeConversionMethod, polymorphismSchema); SerializerFieldInfo result; if (!this._serializers.TryGetValue(key, out result)) { result = new SerializerFieldInfo( this.DefineInitonlyField( "_serializer" + this._serializers.Count, typeof(MessagePackSerializer <>).MakeGenericType(targetType) ), il => { foreach (var construct in schemaRegenerationCodeProvider()) { construct.Evaluate(il); } } ); this._serializers.Add(key, result); } return(result.Field); }
/// <summary> /// Regisgters <see cref="MessagePackSerializer{T}"/> of target type usage to the current emitting session. /// </summary> /// <param name="targetType">The type of the member to be serialized/deserialized.</param> /// <param name="enumMemberSerializationMethod">The enum serialization method of the member to be serialized/deserialized.</param> /// <param name="dateTimeConversionMethod">The date time conversion method of the member to be serialized/deserialized.</param> /// <param name="polymorphismSchema">The schema for polymorphism support.</param> /// <param name="schemaRegenerationCodeProvider">The delegate to provide constructs to emit schema regeneration codes.</param> /// <returns> /// <see cref=" Action{T1,T2}"/> to emit serializer retrieval instructions. /// The 1st argument should be <see cref="TracingILGenerator"/> to emit instructions. /// The 2nd argument should be argument index of the serializer holder, normally 0 (this pointer). /// This value will not be <c>null</c>. /// </returns> public override Action <TracingILGenerator, int> RegisterSerializer( Type targetType, EnumMemberSerializationMethod enumMemberSerializationMethod, DateTimeMemberConversionMethod dateTimeConversionMethod, PolymorphismSchema polymorphismSchema, Func <IEnumerable <ILConstruct> > schemaRegenerationCodeProvider ) { if (this._typeBuilder.IsCreated()) { throw new InvalidOperationException("Type is already built."); } var key = new SerializerFieldKey(targetType, enumMemberSerializationMethod, dateTimeConversionMethod, polymorphismSchema); SerializerFieldInfo result; if (!this._serializers.TryGetValue(key, out result)) { result = new SerializerFieldInfo( this._typeBuilder.DefineField( "_serializer" + this._serializers.Count, typeof(MessagePackSerializer <>).MakeGenericType(targetType), FieldAttributes.Private | FieldAttributes.InitOnly), il => { foreach (var construct in schemaRegenerationCodeProvider()) { construct.Evaluate(il); } } ); this._serializers.Add(key, result); } return ((il, thisIndex) => { il.EmitAnyLdarg(thisIndex); il.EmitLdfld(result.Field); }); }