Ejemplo n.º 1
0
 public void MakeSerializable(
     MutableType proxyType,
     string participantConfigurationID,
     IAssembledTypeIdentifierProvider assembledTypeIdentifierProvider,
     AssembledTypeID typeID)
 {
     // Does nothing.
 }
        public void MakeSerializable(
            MutableType proxyType,
            string participantConfigurationID,
            IAssembledTypeIdentifierProvider assembledTypeIdentifierProvider,
            AssembledTypeID typeID)
        {
            ArgumentUtility.CheckNotNull("proxyType", proxyType);
            ArgumentUtility.CheckNotNullOrEmpty("participantConfigurationID", participantConfigurationID);
            ArgumentUtility.CheckNotNull("assembledTypeIdentifierProvider", assembledTypeIdentifierProvider);

            if (!proxyType.IsTypePipeSerializable())
            {
                return;
            }

            var assembledTypeIDData = assembledTypeIdentifierProvider.GetAssembledTypeIDDataExpression(typeID);

            if (typeof(ISerializable).IsTypePipeAssignableFrom(proxyType))
            {
                // If the mutable type already implements ISerializable, we only need to extend the implementation to include the metadata required for
                // deserialization. Existing fields will be serialized by the base ISerialization implementation. Added fields will be serialized by
                // the TypePipe (ProxySerializationEnabler).
                try
                {
                    proxyType
                    .GetOrAddImplementation(s_getObjectDataMethod)
                    .SetBody(
                        ctx => Expression.Block(
                            ctx.PreviousBody,
                            CreateMetaDataSerializationExpression(
                                ctx.Parameters[0], typeof(ObjectWithDeserializationConstructorProxy), participantConfigurationID, assembledTypeIDData)));
                }
                catch (NotSupportedException)
                {
                    // Overriding and re-implementation failed because the base implementation of GetObjectData is not accessible from the proxy.
                    // Do nothing here; error reporting code will be generated in the ProxySerializationEnabler.
                    // Reasoning: Users often cannot influence the requested type and do not care about any serialization problem.
                }
            }
            else
            {
                // If the mutable type does not implement ISerializable, we need to add the interface and then also serialize all the fields on the object.
                // We cannot add a deserialization constructor because there is no base constructor that we could call. Therefore, ProxySerializationEnabler
                // cannot take care of serializing the added fields, and we thus have to serialize both existing and added fields ourselves via
                // ReflectionSerializationHelper.AddFieldValues.

                proxyType.AddInterface(typeof(ISerializable));

                proxyType.AddExplicitOverride(
                    s_getObjectDataMethod,
                    ctx => Expression.Block(
                        CreateMetaDataSerializationExpression(
                            ctx.Parameters[0], typeof(ObjectWithoutDeserializationConstructorProxy), participantConfigurationID, assembledTypeIDData),
                        Expression.Call(s_addFieldValuesMethod, ctx.Parameters[0], ctx.This)));
            }
        }
Ejemplo n.º 3
0
        public void SetUp()
        {
            _enabler = new ComplexSerializationEnabler();

            _participantConfigurationID          = "configID";
            _assembledTypeIdentifierProviderStub = MockRepository.GenerateStub <IAssembledTypeIdentifierProvider>();
            _typeID = AssembledTypeIDObjectMother.Create();
            _assembledTypeIDData = ExpressionTreeObjectMother.GetSomeExpression();
            _assembledTypeIdentifierProviderStub
            .Stub(_ => _.GetAssembledTypeIDDataExpression(Arg <AssembledTypeID> .Matches(id => id.Equals(_typeID))))
            .Return(_assembledTypeIDData);
        }
Ejemplo n.º 4
0
        public TypeAssembler(
            string participantConfigurationID,
            IEnumerable <IParticipant> participants,
            IMutableTypeFactory mutableTypeFactory,
            IComplexSerializationEnabler complexSerializationEnabler)
        {
            ArgumentUtility.CheckNotNullOrEmpty("participantConfigurationID", participantConfigurationID);
            ArgumentUtility.CheckNotNull("participants", participants);
            ArgumentUtility.CheckNotNull("mutableTypeFactory", mutableTypeFactory);
            ArgumentUtility.CheckNotNull("complexSerializationEnabler", complexSerializationEnabler);


            _participantConfigurationID = participantConfigurationID;
            _participants                = participants.ToList().AsReadOnly();
            _mutableTypeFactory          = mutableTypeFactory;
            _complexSerializationEnabler = complexSerializationEnabler;

            _assembledTypeIdentifierProvider = new AssembledTypeIdentifierProvider(_participants);
        }
Ejemplo n.º 5
0
        private TypeAssembler CreateTypeAssembler(
            IMutableTypeFactory mutableTypeFactory = null,
            IAssembledTypeIdentifierProvider assembledTypeIdentifierProvider = null,
            IComplexSerializationEnabler complexSerializationEnabler         = null,
            string configurationId = "id",
            params IParticipant[] participants)
        {
            mutableTypeFactory = mutableTypeFactory ?? _mutableTypeFactoryMock.Object;
            // Do not fix up assembledTypeIdentifierProvider.
            complexSerializationEnabler = complexSerializationEnabler ?? _complexSerializationEnablerMock.Object;

            var typeAssembler = new TypeAssembler(configurationId, participants.AsOneTime(), mutableTypeFactory, complexSerializationEnabler);

            if (assembledTypeIdentifierProvider != null)
            {
                PrivateInvoke.SetNonPublicField(typeAssembler, "_assembledTypeIdentifierProvider", assembledTypeIdentifierProvider);
            }

            return(typeAssembler);
        }