Ejemplo n.º 1
0
        /// <summary>
        ///		Resets internal states for new type.
        /// </summary>
        /// <param name="targetType">Type of the target.</param>
        protected override void ResetCore(Type targetType)
        {
            var declaringType = new CodeTypeDeclaration(IdentifierUtility.EscapeTypeName(targetType) + "Serializer");

            declaringType.BaseTypes.Add(
                targetType.GetIsEnum()
                                ? typeof(EnumMessagePackSerializer <>).MakeGenericType(targetType)
                                : typeof(MessagePackSerializer <>).MakeGenericType(targetType));
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(GeneratedCodeAttribute)),
                    new CodeAttributeArgument(new CodePrimitiveExpression("MsgPack.Serialization.CodeDomSerializers.CodeDomSerializerBuilder")),
                    new CodeAttributeArgument(new CodePrimitiveExpression(this.GetType().Assembly.GetName().Version.ToString()))
                    )
                );
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerNonUserCodeAttribute)))
                );

            this._declaringTypes.Add(targetType, declaringType);
            this._dependentSerializers.Clear();
            this._cachedFieldInfos.Clear();
            this._cachedMethodBases.Clear();
            this._buildingType = declaringType;

            this.Packer         = CodeDomConstruct.Parameter(typeof(Packer), "packer");
            this.PackToTarget   = CodeDomConstruct.Parameter(targetType, "objectTree");
            this.Unpacker       = CodeDomConstruct.Parameter(typeof(Unpacker), "unpacker");
            this.UnpackToTarget = CodeDomConstruct.Parameter(targetType, "collection");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///		Resets internal states for new type.
        /// </summary>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="baseClass">Type of base class of the target.</param>
        protected override void ResetCore(Type targetType, Type baseClass)
        {
            var declaringType = new CodeTypeDeclaration(IdentifierUtility.EscapeTypeName(targetType) + "Serializer");

            declaringType.BaseTypes.Add(baseClass);
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(GeneratedCodeAttribute)),
                    new CodeAttributeArgument(new CodePrimitiveExpression("MsgPack.Serialization.CodeDomSerializers.CodeDomSerializerBuilder")),
                    new CodeAttributeArgument(new CodePrimitiveExpression(this.GetType().Assembly.GetName().Version.ToString()))
                    )
                );
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerNonUserCodeAttribute)))
                );

            this._declaringTypes.Add(targetType, declaringType);
            this._dependentSerializers.Clear();
            this._cachedFieldInfos.Clear();
            this._cachedMethodBases.Clear();
            this._buildingType = declaringType;

            this.Packer         = CodeDomConstruct.Parameter(typeof(Packer), "packer");
            this.PackToTarget   = CodeDomConstruct.Parameter(targetType, "objectTree");
            this.Unpacker       = CodeDomConstruct.Parameter(typeof(Unpacker), "unpacker");
            this.UnpackToTarget = CodeDomConstruct.Parameter(targetType, "collection");
            var traits = targetType.GetCollectionTraits();

            if (traits.ElementType != null)
            {
                this.CollectionToBeAdded = CodeDomConstruct.Parameter(targetType, "collection");
                this.ItemToAdd           = CodeDomConstruct.Parameter(traits.ElementType, "item");
                this.InitialCapacity     = CodeDomConstruct.Parameter(typeof(int), "initialCapacity");
            }
        }
Ejemplo n.º 3
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 FieldBasedSerializerEmitter(ModuleBuilder host, int?sequence, Type targetType, bool isDebuggable)
            : base()
        {
            Contract.Requires(host != null);
            Contract.Requires(targetType != null);

            string typeName =
#if !NETFX_35
                String.Join(
                    Type.Delimiter.ToString(),
                    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(MessagePackSerializer <>).MakeGenericType(targetType)
                    );

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

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

            this._unpackFromMethodBuilder =
                this._typeBuilder.DefineMethod(
                    "UnpackFromCore",
                    MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig,
                    CallingConventions.HasThis,
                    targetType,
                    UnpackFromCoreParameterTypes
                    );

            this._typeBuilder.DefineMethodOverride(this._packMethodBuilder, this._typeBuilder.BaseType.GetMethod(this._packMethodBuilder.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
            this._typeBuilder.DefineMethodOverride(this._unpackFromMethodBuilder, this._typeBuilder.BaseType.GetMethod(this._unpackFromMethodBuilder.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
            this._serializers  = new Dictionary <RuntimeTypeHandle, FieldBuilder>();
            this._isDebuggable = isDebuggable;
        }
Ejemplo n.º 4
0
        /// <summary>
        ///		Resets internal states for new type.
        /// </summary>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="baseClass">Type of base class of the target.</param>
        protected override void ResetCore(Type targetType, Type baseClass)
        {
            var declaringType = new CodeTypeDeclaration(IdentifierUtility.EscapeTypeName(targetType) + "Serializer");

            declaringType.BaseTypes.Add(baseClass);
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(GeneratedCodeAttribute)),
                    new CodeAttributeArgument(new CodePrimitiveExpression("MsgPack.Serialization.CodeDomSerializers.CodeDomSerializerBuilder")),
                    new CodeAttributeArgument(new CodePrimitiveExpression(this.GetType().Assembly.GetName().Version.ToString()))
                    )
                );
            declaringType.CustomAttributes.Add(
                new CodeAttributeDeclaration(new CodeTypeReference(typeof(DebuggerNonUserCodeAttribute)))
                );

            this._targetType = targetType;
            this._declaringTypes.Add(targetType, declaringType);
            this._dependentSerializers.Clear();
            this._cachedTargetFields.Clear();
            this._cachedPropertyAccessors.Clear();
            this._buildingType = declaringType;

            var targetTypeDefinition = TypeDefinition.Object(targetType);

            this.Packer          = CodeDomConstruct.Parameter(TypeDefinition.PackerType, "packer");
            this.PackToTarget    = CodeDomConstruct.Parameter(targetTypeDefinition, "objectTree");
            this.NullCheckTarget = CodeDomConstruct.Parameter(targetTypeDefinition, "objectTree");
            this.Unpacker        = CodeDomConstruct.Parameter(TypeDefinition.UnpackerType, "unpacker");
            this.IndexOfItem     = CodeDomConstruct.Parameter(TypeDefinition.Int32Type, "indexOfItem");
            this.ItemsCount      = CodeDomConstruct.Parameter(TypeDefinition.Int32Type, "itemsCount");
            this.UnpackToTarget  = CodeDomConstruct.Parameter(targetTypeDefinition, "collection");
            var traits = targetType.GetCollectionTraits(CollectionTraitOptions.Full, this.SerializationContext.CompatibilityOptions.AllowNonCollectionEnumerableTypes);

            if (traits.ElementType != null)
            {
                this.CollectionToBeAdded = CodeDomConstruct.Parameter(targetTypeDefinition, "collection");
                this.ItemToAdd           = CodeDomConstruct.Parameter(traits.ElementType, "item");
                if (traits.DetailedCollectionType == CollectionDetailedKind.GenericDictionary
#if !NET35 && !NET40
                    || traits.DetailedCollectionType == CollectionDetailedKind.GenericReadOnlyDictionary
#endif // !NET35 && !NET40
                    )
                {
                    this.KeyToAdd   = CodeDomConstruct.Parameter(traits.ElementType.GetGenericArguments()[0], "key");
                    this.ValueToAdd = CodeDomConstruct.Parameter(traits.ElementType.GetGenericArguments()[1], "value");
                }
                else
                {
                    this.KeyToAdd   = null;
                    this.ValueToAdd = null;
                }
                this.InitialCapacity = CodeDomConstruct.Parameter(TypeDefinition.Int32Type, "initialCapacity");
            }
        }
Ejemplo n.º 5
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
        }
Ejemplo n.º 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
        }