public static MemoryStream AdvancedBinaryFormatterObjectToStream(List <AdvancedBinaryFormatterObject> abfoList)
        {
            MemoryStream resultMS = new MemoryStream();

            InternalFE formatterEnums = new InternalFE();

            formatterEnums.FEtypeFormat         = FormatterTypeStyle.TypesAlways;
            formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
            formatterEnums.FEassemblyFormat     = FormatterAssemblyStyle.Simple;
            formatterEnums.FEsecurityLevel      = TypeFilterLevel.Low;
            ISurrogateSelector  m_surrogates = null;
            StreamingContext    m_context    = new StreamingContext(StreamingContextStates.All);
            SerializationBinder m_binder     = null;
            //bool fCheck = false;
            //HeaderHandler handler = null;

            ObjectWriter   objectWriter = new ObjectWriter(m_surrogates, m_context, formatterEnums, m_binder);
            __BinaryWriter binaryWriter = new __BinaryWriter(resultMS, objectWriter, formatterEnums.FEtypeFormat);

            List <int> asmArray = new List <int>();

            foreach (AdvancedBinaryFormatterObject abfo in abfoList)
            {
                var currentObjInfo = abfo.Data;
                if (currentObjInfo.GetType() == typeof(BinaryObjectWithMapTyped))
                {
                    if (currentObjInfo.binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMapTypedAssemId)
                    {
                        if (asmArray.IndexOf(currentObjInfo.assemId) == -1 && currentObjInfo.assemId != 0)
                        {
                            asmArray.Add(currentObjInfo.assemId);
                        }
                        else
                        {
                            currentObjInfo.assemId = 0;
                        }
                    }
                }

                currentObjInfo.Write(binaryWriter);
                if (abfo.ArrayBytes != null)
                {
                    // this is for arrays when we have more data:

                    /*
                     * BinaryHeaderEnum.Array:
                     * BinaryHeaderEnum.ArraySinglePrimitive:
                     * BinaryHeaderEnum.ArraySingleObject:
                     * BinaryHeaderEnum.ArraySingleString:
                     */
                    binaryWriter.WriteBytes(abfo.ArrayBytes);
                }
            }
            return(resultMS);
        }
Example #2
0
        [System.Security.SecurityCritical]  // auto-generated
        internal void Serialize(Stream serializationStream, Object graph, Header[] headers, bool fCheck)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream", Environment.GetResourceString("ArgumentNull_WithParamName", serializationStream));
            }
            Contract.EndContractBlock();
            SerTrace.Log(this, "Serialize Entry");

            InternalFE formatterEnums = new InternalFE();

            formatterEnums.FEtypeFormat         = m_typeFormat;
            formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
            formatterEnums.FEassemblyFormat     = m_assemblyFormat;

            ObjectWriter   sow          = new ObjectWriter(m_surrogates, m_context, formatterEnums, m_binder);
            __BinaryWriter binaryWriter = new __BinaryWriter(serializationStream, sow, m_typeFormat);

            sow.Serialize(graph, headers, binaryWriter, fCheck);
            m_crossAppDomainArray = sow.crossAppDomainArray;
        }