internal static WriteObjectInfo Serialize(object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SoapAttributeInfo attributeInfo, ObjectWriter objectWriter)
 {
     WriteObjectInfo objectInfo = GetObjectInfo(serObjectInfoInit);
     objectInfo.InitSerialize(obj, surrogateSelector, context, serObjectInfoInit, converter, attributeInfo, objectWriter);
     return objectInfo;
 }
 private void InitSiWrite(ObjectWriter objectWriter)
 {
     if (this.si.FullTypeName.Equals("FormatterWrapper"))
     {
         this.obj = this.si.GetValue("__WrappedObject", Converter.typeofObject);
         this.InitSerialize(this.obj, this.surrogateSelector, this.context, this.serObjectInfoInit, this.converter, null, objectWriter);
     }
     else
     {
         SerializationInfoEnumerator enumerator = null;
         this.isSi = true;
         enumerator = this.si.GetEnumerator();
         int memberCount = this.si.MemberCount;
         this.cache = new SerObjectInfoCache();
         this.cache.memberNames = new string[memberCount];
         this.cache.memberTypes = new Type[memberCount];
         this.memberData = new object[memberCount];
         this.cache.fullTypeName = this.si.FullTypeName;
         this.cache.assemblyString = this.si.AssemblyName;
         enumerator = this.si.GetEnumerator();
         for (int i = 0; enumerator.MoveNext(); i++)
         {
             this.cache.memberNames[i] = enumerator.Name;
             this.cache.memberTypes[i] = enumerator.ObjectType;
             this.memberData[i] = enumerator.Value;
         }
         this.isNamed = true;
         this.isTyped = false;
     }
 }
 internal void InitSerialize(object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SoapAttributeInfo attributeInfo, ObjectWriter objectWriter)
 {
     this.context = context;
     this.obj = obj;
     this.serObjectInfoInit = serObjectInfoInit;
     this.parentMemberAttributeInfo = attributeInfo;
     this.surrogateSelector = surrogateSelector;
     this.converter = converter;
     if (RemotingServices.IsTransparentProxy(obj))
     {
         this.objectType = Converter.typeofMarshalByRefObject;
     }
     else
     {
         this.objectType = obj.GetType();
     }
     if (this.objectType.IsArray)
     {
         this.arrayElemObjectInfo = Serialize(this.objectType.GetElementType(), surrogateSelector, context, serObjectInfoInit, converter, null);
         this.typeAttributeInfo = this.GetTypeAttributeInfo();
         this.isArray = true;
         this.InitNoMembers();
     }
     else
     {
         ISurrogateSelector selector;
         this.typeAttributeInfo = this.GetTypeAttributeInfo();
         objectWriter.ObjectManager.RegisterObject(obj);
         if ((surrogateSelector != null) && ((this.serializationSurrogate = surrogateSelector.GetSurrogate(this.objectType, context, out selector)) != null))
         {
             this.si = new SerializationInfo(this.objectType, converter);
             if (!this.objectType.IsPrimitive)
             {
                 this.serializationSurrogate.GetObjectData(obj, this.si, context);
             }
             this.InitSiWrite(objectWriter);
         }
         else if (obj is ISerializable)
         {
             if (!this.objectType.IsSerializable)
             {
                 throw new SerializationException(string.Format(CultureInfo.CurrentCulture, SoapUtil.GetResourceString("Serialization_NonSerType"), new object[] { this.objectType.FullName, this.objectType.Module.Assembly.FullName }));
             }
             this.si = new SerializationInfo(this.objectType, converter);
             ((ISerializable) obj).GetObjectData(this.si, context);
             this.InitSiWrite(objectWriter);
         }
         else
         {
             this.InitMemberInfo();
         }
     }
 }
    	// Commences the process of serializing the entire graph.  All of the data (in the appropriate format)
    	// is emitted onto the stream.

		public void Serialize(Stream serializationStream, Object graph, Header[] headers)
		{
			InternalST.InfoSoap("Enter SoapFormatter.Serialize ");
            if (serializationStream==null) {
                throw new ArgumentNullException("serializationStream");
            }

			InternalST.Soap( this, "Serialize Entry");
			InternalFE formatterEnums = new InternalFE();
			formatterEnums.FEtypeFormat = m_typeFormat;
			formatterEnums.FEtopObject = m_topObject;
			formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Soap;
			formatterEnums.FEassemblyFormat = m_assemblyFormat;
			ObjectWriter sow = new ObjectWriter(serializationStream, m_surrogates, m_context, formatterEnums);
			sow.Serialize(graph, headers, new SoapWriter(serializationStream));
			InternalST.InfoSoap("Leave SoapFormatter.Serialize ");			
		}
        private void InitSiWrite(ObjectWriter objectWriter)
        {
            InternalST.Soap( this, objectInfoId," InitSiWrite Entry ");
            // FormatterWrapper instructs the Formatters to use the
            // __WrappedObject has the real object. This is a way
            // to get Surrogates to return a real object.
            if (si.FullTypeName.Equals("FormatterWrapper"))
            {
                obj = si.GetValue("__WrappedObject", Converter.typeofObject);
                InitSerialize(obj, surrogateSelector, context, serObjectInfoInit, converter, null, objectWriter);
            }
            else
            {

                SerializationInfoEnumerator siEnum = null;
                isSi = true;        
                siEnum = si.GetEnumerator();
                int infoLength = 0;

                infoLength = si.MemberCount;

                int count = infoLength;

                // For ISerializable cache cannot be saved because each object instance can have different values
                // BinaryWriter only puts the map on the wire if the ISerializable map cannot be reused.
                cache = new SerObjectInfoCache();
                cache.memberNames = new String[count];
                cache.memberTypes = new Type[count];
                memberData = new Object[count];

                cache.fullTypeName = si.FullTypeName;
                cache.assemblyString = si.AssemblyName;
                siEnum = si.GetEnumerator();
                for (int i=0; siEnum.MoveNext(); i++)
                {
                    cache.memberNames[i] = siEnum.Name;
                    cache.memberTypes[i] = siEnum.ObjectType;
                    memberData[i] = siEnum.Value;
                    InternalST.Soap( this,objectInfoId+" ",objectType," InitSiWrite ",cache.memberNames[i]," Type ",cache.memberTypes[i]," data ",memberData[i]);                          
                }

                isNamed = true;
                isTyped = false;
            }
            InternalST.Soap(this, objectInfoId," InitSiWrite Exit ");      
        }
        // Write constructor
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SoapAttributeInfo attributeInfo, ObjectWriter objectWriter)
        {
            InternalST.Soap( this, objectInfoId," Constructor 1 ",obj);        
            this.context = context;
            this.obj = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            this.parentMemberAttributeInfo = attributeInfo;
            this.surrogateSelector = surrogateSelector;
            this.converter = converter;
            ISurrogateSelector surrogateSelectorTemp;

            if (RemotingServices.IsTransparentProxy(obj))
                objectType = Converter.typeofMarshalByRefObject;
            else
                objectType = obj.GetType();

            if (objectType.IsArray)
            {
                arrayElemObjectInfo = Serialize(objectType.GetElementType(), surrogateSelector, context, serObjectInfoInit, converter, null);
                typeAttributeInfo = GetTypeAttributeInfo();
                isArray = true;
                InitNoMembers();
                return;
            }

            InternalST.Soap( this, objectInfoId," Constructor 1 trace 2");

            typeAttributeInfo = GetTypeAttributeInfo();

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                InternalST.Soap( this, objectInfoId," Constructor 1 trace 3");         
                si = new SerializationInfo(objectType, converter);
                if (!objectType.IsPrimitive)
                    serializationSurrogate.GetObjectData(obj, si, context);
                InitSiWrite(objectWriter);
            }
            else if (obj is ISerializable)
            {
                if (!objectType.IsSerializable)
                {
                    throw new SerializationException(String.Format(CultureInfo.CurrentCulture, SoapUtil.GetResourceString("Serialization_NonSerType"), 
                                                                   objectType.FullName, objectType.Module.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter);
                ((ISerializable)obj).GetObjectData(si, context);
                InternalST.Soap( this, objectInfoId," Constructor 1 trace 4 ISerializable "+objectType);                       
                InitSiWrite(objectWriter);
            }
            else
            {
                InternalST.Soap(this, objectInfoId," Constructor 1 trace 5");
                InitMemberInfo();
            }
        }