/// <summary>
        /// Constructs a new <see cref="PropertyBasedSerializationSurrogate"/>.
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> to use.</param>
        /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param>
        public PropertyBasedSerializationSurrogate(FudgeContext context, TypeData typeData)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (typeData == null)
                throw new ArgumentNullException("typeData");
            if (!CanHandle(typeData))
                throw new ArgumentOutOfRangeException("typeData", "PropertyBasedSerializationSurrogate cannot handle " + typeData.Type.FullName);

            Debug.Assert(typeData.DefaultConstructor != null);      // Should have been caught in CanHandle()

            var constructor = typeData.DefaultConstructor;
            this.memberSerializer = new MemberSerializerMixin(context, typeData, typeData.Properties, new BeforeAfterSerializationMixin(context, typeData), () => constructor.Invoke(null));
        }
示例#2
0
        /// <summary>
        /// Constructs a new <see cref="ImmutableSurrogate"/>.
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> to use.</param>
        /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param>
        public ImmutableSurrogate(FudgeContext context, TypeData typeData)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (typeData == null)
                throw new ArgumentNullException("typeData");
            if (!CanHandle(typeData))
                throw new ArgumentOutOfRangeException("typeData", "ImmutableSurrogate cannot handle " + typeData.Type.FullName);

            this.context = context;
            this.type = typeData.Type;
            this.constructor = FindConstructor(typeData.Constructors, typeData.Properties, out constructorParams);
            Debug.Assert(constructor != null);  // Else how did it pass CanHandle?

            this.memberSerializer = new MemberSerializerMixin(context, typeData, typeData.Properties, new BeforeAfterSerializationMixin(context, typeData), null);
        }
        /// <summary>
        /// Constructs a new <see cref="SerializableAttributeSurrogate"/>.
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> to use.</param>
        /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param>
        public SerializableAttributeSurrogate(FudgeContext context, TypeData typeData)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (typeData == null)
                throw new ArgumentNullException("typeData");
            if (!CanHandle(typeData))
                throw new ArgumentOutOfRangeException("typeData", "SerializableAttributeSurrogate cannot handle " + typeData.Type.FullName);

            this.type = typeData.Type;

            var fields = from field in typeData.Fields
                         where field.GetCustomAttribute<NonSerializedAttribute>() == null
                         select field;

            var type = typeData.Type;       // So the closure in the lambda below is as tight as possible
            var beforeAfterMixin = new BeforeAfterSerializationMixin(context, typeData);
            this.memberSerializer = new MemberSerializerMixin(context, typeData, fields, beforeAfterMixin, () => FormatterServices.GetUninitializedObject(type));
        }
        /// <summary>
        /// Constructs a new instance for a specific type
        /// </summary>
        /// <param name="context"><see cref="FudgeContext"/> for this surrogate.</param>
        /// <param name="typeData"><see cref="TypeData"/> describing the type to serialize.</param>
        public DataContractSurrogate(FudgeContext context, TypeData typeData)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (typeData == null)
                throw new ArgumentNullException("typeData");
            if (!CanHandle(typeData))
                throw new ArgumentOutOfRangeException("typeData", "ImmutableSurrogate cannot handle " + typeData.Type.FullName);

            this.context = context;
            this.type = typeData.Type;

            Debug.Assert(typeData.DefaultConstructor != null);      // Should have been caught in CanHandle()

            var properties = from prop in typeData.Properties.Concat(typeData.Fields)
                             where prop.GetCustomAttribute<DataMemberAttribute>() != null
                             select prop;

            var type = typeData.Type;       // So the closure in the lambda below is as tight as possible
            var beforeAfterMixin = new BeforeAfterSerializationMixin(context, typeData);
            this.memberSerializer = new MemberSerializerMixin(context, typeData, properties, beforeAfterMixin, () => FormatterServices.GetUninitializedObject(type));
        }