예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializerEmitter"/> class.
        /// </summary>
        /// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
        /// <param name="specification">The specification of the serializer.</param>
        /// <param name="baseClass">Type of the base class of the serializer.</param>
        /// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
        public SerializerEmitter(ModuleBuilder host, SerializerSpecification specification, Type baseClass, bool isDebuggable)
        {
            Contract.Requires(host != null);
            Contract.Requires(specification != null);
            Contract.Requires(baseClass != null);

            Tracer.Emit.TraceEvent(Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", specification.SerializerTypeFullName);

            this._methodTable   = new Dictionary <string, MethodBuilder>();
            this._fieldTable    = new Dictionary <string, FieldBuilder>();
            this._specification = specification;
            this._host          = host;
            this._typeBuilder   =
                host.DefineType(
                    specification.SerializerTypeFullName,
                    TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
                    baseClass
                    );

#if DEBUG
            Contract.Assert(this._typeBuilder.BaseType != null, "baseType != null");
#endif // DEBUG
            this._isDebuggable = isDebuggable;

#if !NETFX_35 && !NETSTANDARD1_1 && !NETSTANDARD1_3
            if (isDebuggable && SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump(host.Assembly as AssemblyBuilder);
            }
#endif // !NETFX_35 && !NETSTANDARD1_1 && !NETSTANDARD1_3
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldBasedSerializerEmitter"/> class.
        /// </summary>
        /// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
        /// <param name="specification">The specification of the serializer.</param>
        /// <param name="baseClass">Type of the base class of the serializer.</param>
        /// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
        public FieldBasedSerializerEmitter(ModuleBuilder host, SerializerSpecification specification, Type baseClass, bool isDebuggable)
        {
            Contract.Requires(host != null);
            Contract.Requires(specification != null);
            Contract.Requires(baseClass != null);

            Tracer.Emit.TraceEvent(Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", specification.SerializerTypeFullName);
            this._typeBuilder =
                host.DefineType(
                    specification.SerializerTypeFullName,
                    TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
                    baseClass
                    );

            this._defaultConstructorBuilder = this._typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
            this._contextConstructorBuilder = this._typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ConstructorParameterTypes);

            this._traits = specification.TargetCollectionTraits;
            var baseType = this._typeBuilder.BaseType;

#if DEBUG
            Contract.Assert(baseType != null, "baseType != null");
#endif
            this._serializers  = new Dictionary <SerializerFieldKey, SerializerFieldInfo>();
            this._fieldInfos   = new Dictionary <RuntimeFieldHandle, FieldBuilder>();
            this._methodBases  = new Dictionary <RuntimeMethodHandle, FieldBuilder>();
            this._isDebuggable = isDebuggable;

#if !SILVERLIGHT && !NETFX_35
            if (isDebuggable && SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump(host.Assembly as AssemblyBuilder);
            }
#endif
        }
예제 #3
0
        /// <summary>
        ///		Initializes a new instance of the <see cref="ExpressionTreeSerializerBuilder{TObject}"/> class.
        /// </summary>
        public ExpressionTreeSerializerBuilder()
        {
#if !NETFX_CORE && !SILVERLIGHT
            if (SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump();
                this._typeBuilder = SerializerDebugging.NewTypeBuilder(typeof(TObject));
            }
#endif
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldBasedSerializerEmitter"/> class.
        /// </summary>
        /// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
        /// <param name="sequence">The sequence number to name new type.</param>
        /// <param name="targetType">Type of the serialization target.</param>
        /// <param name="baseClass">Type of the base class of the serializer.</param>
        /// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
        public FieldBasedSerializerEmitter(ModuleBuilder host, int?sequence, Type targetType, Type baseClass, bool isDebuggable)
        {
            Contract.Requires(host != null);
            Contract.Requires(targetType != null);
            Contract.Requires(baseClass != null);

            string typeName =
#if !NETFX_35
                String.Join(
                    Type.Delimiter.ToString(CultureInfo.InvariantCulture),
                    typeof(SerializerEmitter).Namespace,
                    "Generated",
                    IdentifierUtility.EscapeTypeName(targetType) + "Serializer" + sequence
                    );
#else
                String.Join(
                    Type.Delimiter.ToString(),
                    new string[]
            {
                typeof(SerializerEmitter).Namespace,
                "Generated",
                IdentifierUtility.EscapeTypeName(targetType) + "Serializer" + sequence
            }
                    );
#endif
            Tracer.Emit.TraceEvent(Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", typeName);
            this._typeBuilder =
                host.DefineType(
                    typeName,
                    TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
                    baseClass
                    );

            this._defaultConstructorBuilder = this._typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
            this._contextConstructorBuilder = this._typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ConstructorParameterTypes);

            this._targetType = targetType;
            this._traits     = targetType.GetCollectionTraits();
            var baseType = this._typeBuilder.BaseType;
#if DEBUG
            Contract.Assert(baseType != null, "baseType != null");
#endif
            this._serializers  = new Dictionary <SerializerFieldKey, SerializerFieldInfo>();
            this._fieldInfos   = new Dictionary <RuntimeFieldHandle, FieldBuilder>();
            this._methodBases  = new Dictionary <RuntimeMethodHandle, FieldBuilder>();
            this._isDebuggable = isDebuggable;

#if !SILVERLIGHT && !NETFX_35
            if (isDebuggable && SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump(host.Assembly as AssemblyBuilder);
            }
#endif
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldBasedSerializerEmitter"/> class.
        /// </summary>
        /// <param name="context">A <see cref="SerializationContext"/>.</param>
        /// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
        /// <param name="specification">The specification of the serializer.</param>
        /// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
        public FieldBasedEnumSerializerEmitter(SerializationContext context, ModuleBuilder host, SerializerSpecification specification, bool isDebuggable)
        {
            Contract.Requires(host != null);
            Contract.Requires(specification != null);

            Tracer.Emit.TraceEvent(Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", specification.SerializerTypeFullName);
            this._typeBuilder =
                host.DefineType(
                    specification.SerializerTypeFullName,
                    TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout |
                    TypeAttributes.BeforeFieldInit,
                    typeof(EnumMessagePackSerializer <>).MakeGenericType(specification.TargetType)
                    );

            this._contextConstructorBuilder =
                this._typeBuilder.DefineConstructor(
                    MethodAttributes.Public,
                    CallingConventions.Standard,
                    ContextConstructorParameterTypes
                    );
            this._defaultEnumSerializationMethod = context.EnumSerializationMethod;
            this._contextAndEnumSerializationMethodConstructorBuilder =
                this._typeBuilder.DefineConstructor(
                    MethodAttributes.Public,
                    CallingConventions.Standard,
                    ContextAndEnumSerializationMethodConstructorParameterTypes
                    );

            this._packUnderlyingValueToMethodBuilder =
                this._typeBuilder.DefineMethod(
                    "PackUnderlyingValueTo",
                    MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig,
                    CallingConventions.HasThis,
                    typeof(void),
                    new[] { typeof(Packer), specification.TargetType }
                    );

            this._unpackFromUnderlyingValueMethodBuilder =
                this._typeBuilder.DefineMethod(
                    "UnpackFromUnderlyingValue",
                    MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig,
                    CallingConventions.HasThis,
                    specification.TargetType,
                    UnpackFromUnderlyingValueParameterTypes
                    );

            var baseType = this._typeBuilder.BaseType;

#if DEBUG
            Contract.Assert(baseType != null, "baseType != null");
#endif
            this._typeBuilder.DefineMethodOverride(
                this._packUnderlyingValueToMethodBuilder,
                baseType.GetMethod(
                    this._packUnderlyingValueToMethodBuilder.Name,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                    )
                );
            this._typeBuilder.DefineMethodOverride(
                this._unpackFromUnderlyingValueMethodBuilder,
                baseType.GetMethod(
                    this._unpackFromUnderlyingValueMethodBuilder.Name,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                    )
                );
            this._isDebuggable = isDebuggable;

#if !SILVERLIGHT && !NETFX_35
            if (isDebuggable && SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump(host.Assembly as AssemblyBuilder);
            }
#endif
        }
예제 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldBasedSerializerEmitter"/> class.
        /// </summary>
        /// <param name="host">The host <see cref="ModuleBuilder"/>.</param>
        /// <param name="sequence">The sequence number to name new type.</param>
        /// <param name="targetType">Type of the serialization target.</param>
        /// <param name="isDebuggable">Set to <c>true</c> when <paramref name="host"/> is debuggable.</param>
        public FieldBasedEnumSerializerEmitter(ModuleBuilder host, int?sequence, Type targetType, bool isDebuggable)
        {
            Contract.Requires(host != null);
            Contract.Requires(targetType != null);

            this._constructorParameterTypes =
                new[]
            {
                typeof(SerializationContext),
                typeof(EnumSerializationMethod)
            };

            string typeName =
#if !NETFX_35
                String.Join(
                    Type.Delimiter.ToString(CultureInfo.InvariantCulture),
                    typeof(SerializerEmitter).Namespace,
                    "Generated",
                    IdentifierUtility.EscapeTypeName(targetType) + "Serializer" + sequence
                    );
#else
                String.Join(
                    Type.Delimiter.ToString(),
                    new string[]
            {
                typeof(SerializerEmitter).Namespace,
                "Generated",
                IdentifierUtility.EscapeTypeName(targetType) + "Serializer" + sequence
            }
                    );
#endif
            Tracer.Emit.TraceEvent(Tracer.EventType.DefineType, Tracer.EventId.DefineType, "Create {0}", typeName);
            this._typeBuilder =
                host.DefineType(
                    typeName,
                    TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.UnicodeClass | TypeAttributes.AutoLayout |
                    TypeAttributes.BeforeFieldInit,
                    typeof(EnumMessagePackSerializer <>).MakeGenericType(targetType)
                    );

            this._contextConstructorBuilder = this._typeBuilder.DefineConstructor(
                MethodAttributes.Public,
                CallingConventions.Standard,
                this._constructorParameterTypes
                );

            this._packUnderlyingValueToMethodBuilder =
                this._typeBuilder.DefineMethod(
                    "PackUnderlyingValueTo",
                    MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig,
                    CallingConventions.HasThis,
                    typeof(void),
                    new[] { typeof(Packer), targetType }
                    );

            this._unpackFromUnderlyingValueMethodBuilder =
                this._typeBuilder.DefineMethod(
                    "UnpackFromUnderlyingValue",
                    MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig,
                    CallingConventions.HasThis,
                    targetType,
                    UnpackFromUnderlyingValueParameterTypes
                    );

            var baseType = this._typeBuilder.BaseType;
#if DEBUG
            Contract.Assert(baseType != null, "baseType != null");
#endif
            this._typeBuilder.DefineMethodOverride(
                this._packUnderlyingValueToMethodBuilder,
                baseType.GetMethod(
                    this._packUnderlyingValueToMethodBuilder.Name,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                    )
                );
            this._typeBuilder.DefineMethodOverride(
                this._unpackFromUnderlyingValueMethodBuilder,
                baseType.GetMethod(
                    this._unpackFromUnderlyingValueMethodBuilder.Name,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                    )
                );
            this._isDebuggable = isDebuggable;

#if !SILVERLIGHT && !NETFX_35
            if (isDebuggable && SerializerDebugging.DumpEnabled)
            {
                SerializerDebugging.PrepareDump(host.Assembly as AssemblyBuilder);
            }
#endif
        }