private void InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
        {
            RuntimeTypeHandle typeHandle = isDeclaredType ? declaredTypeHandle : Type.GetTypeHandle(obj);
            object            oldObj     = obj;
            int  oldObjId       = 0;
            Type typeFromHandle = Type.GetTypeFromHandle(typeHandle);
            Type surrogatedType = this.GetSurrogatedType(Type.GetTypeFromHandle(declaredTypeHandle));

            declaredTypeHandle = surrogatedType.TypeHandle;
            obj        = DataContractSerializer.SurrogateToDataContractType(this.dataContractSurrogate, obj, surrogatedType, ref typeFromHandle);
            typeHandle = typeFromHandle.TypeHandle;
            if (oldObj != obj)
            {
                oldObjId = base.SerializedObjects.ReassignId(0, oldObj, obj);
            }
            if (writeXsiType)
            {
                surrogatedType = Globals.TypeOfObject;
                this.SerializeWithXsiType(xmlWriter, obj, typeHandle, typeFromHandle, -1, surrogatedType.TypeHandle, surrogatedType);
            }
            else if (declaredTypeHandle.Equals(typeHandle))
            {
                DataContract dataContract = this.GetDataContract(typeHandle, typeFromHandle);
                base.SerializeWithoutXsiType(dataContract, xmlWriter, obj, declaredTypeHandle);
            }
            else
            {
                this.SerializeWithXsiType(xmlWriter, obj, typeHandle, typeFromHandle, -1, declaredTypeHandle, surrogatedType);
            }
            if (oldObj != obj)
            {
                base.SerializedObjects.ReassignId(oldObjId, obj, oldObj);
            }
        }
Esempio n. 2
0
        void InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
        {
            RuntimeTypeHandle objTypeHandle = isDeclaredType ? declaredTypeHandle : Type.GetTypeHandle(obj);
            object            oldObj        = obj;
            int  objOldId     = 0;
            Type objType      = Type.GetTypeFromHandle(objTypeHandle);
            Type declaredType = GetSurrogatedType(Type.GetTypeFromHandle(declaredTypeHandle));

            if (TD.DCSerializeWithSurrogateStartIsEnabled())
            {
                TD.DCSerializeWithSurrogateStart(declaredType.FullName);
            }

            declaredTypeHandle = declaredType.TypeHandle;
            obj           = DataContractSerializer.SurrogateToDataContractType(dataContractSurrogate, obj, declaredType, ref objType);
            objTypeHandle = objType.TypeHandle;
            if (oldObj != obj)
            {
                objOldId = SerializedObjects.ReassignId(0, oldObj, obj);
            }

            if (writeXsiType)
            {
                declaredType = Globals.TypeOfObject;
                SerializeWithXsiType(xmlWriter, obj, objTypeHandle, objType, -1, declaredType.TypeHandle, declaredType);
            }
            else if (declaredTypeHandle.Equals(objTypeHandle))
            {
                DataContract contract = GetDataContract(objTypeHandle, objType);
                SerializeWithoutXsiType(contract, xmlWriter, obj, declaredTypeHandle);
            }
            else
            {
                SerializeWithXsiType(xmlWriter, obj, objTypeHandle, objType, -1, declaredTypeHandle, declaredType);
            }
            if (oldObj != obj)
            {
                SerializedObjects.ReassignId(objOldId, obj, oldObj);
            }


            if (TD.DCSerializeWithSurrogateStopIsEnabled())
            {
                TD.DCSerializeWithSurrogateStop();
            }
        }