The Labs \ Source Viewer \ SSCLI \ System.Runtime.Serialization.Formatters.Binary \ BinaryFormatter

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class: BinaryFormatter
  18. **
  19. **
  20. ** Purpose: Soap XML Formatter
  21. **
  22. **
  23. ===========================================================*/
  24. namespace System.Runtime.Serialization.Formatters.Binary
  25. {
  26.    
  27.     using System;
  28.     using System.IO;
  29.     using System.Reflection;
  30.     using System.Globalization;
  31.     using System.Collections;
  32.     using System.Runtime.Serialization.Formatters;
  33.     using System.Runtime.Remoting;
  34.     using System.Runtime.Remoting.Proxies;
  35.     using System.Runtime.Remoting.Messaging;
  36.     using System.Runtime.Serialization;
  37.     using System.Security.Permissions;
  38.    
  39.     [System.Runtime.InteropServices.ComVisible(true)]
  40.     public sealed class BinaryFormatter : IRemotingFormatter
  41.     {
  42.        
  43.         internal ISurrogateSelector m_surrogates;
  44.         internal StreamingContext m_context;
  45.         internal SerializationBinder m_binder;
  46.         //internal FormatterTypeStyle m_typeFormat = FormatterTypeStyle.TypesWhenNeeded;
  47.         internal FormatterTypeStyle m_typeFormat = FormatterTypeStyle.TypesAlways;
  48.         // For version resiliency, always put out types
  49.         internal FormatterAssemblyStyle m_assemblyFormat = FormatterAssemblyStyle.Simple;
  50.         internal TypeFilterLevel m_securityLevel = TypeFilterLevel.Full;
  51.         internal object[] m_crossAppDomainArray = null;
  52.        
  53.         // Property which specifies how types are serialized,
  54.         // FormatterTypeStyle Enum specifies options
  55.         public FormatterTypeStyle TypeFormat {
  56.             get { return m_typeFormat; }
  57.             set { m_typeFormat = value; }
  58.         }
  59.        
  60.         // Property which specifies how types are serialized,
  61.         // FormatterAssemblyStyle Enum specifies options
  62.         public FormatterAssemblyStyle AssemblyFormat {
  63.             get { return m_assemblyFormat; }
  64.             set { m_assemblyFormat = value; }
  65.         }
  66.        
  67.         // Property which specifies the security level of formatter
  68.         // TypeFilterLevel Enum specifies options
  69.         public TypeFilterLevel FilterLevel {
  70.             get { return m_securityLevel; }
  71.             set { m_securityLevel = value; }
  72.         }
  73.        
  74.         public ISurrogateSelector SurrogateSelector {
  75.             get { return m_surrogates; }
  76.             set { m_surrogates = value; }
  77.         }
  78.        
  79.         public SerializationBinder Binder {
  80.             get { return m_binder; }
  81.             set { m_binder = value; }
  82.         }
  83.        
  84.         public StreamingContext Context {
  85.             get { return m_context; }
  86.             set { m_context = value; }
  87.         }
  88.        
  89.         // Constructor
  90.         public BinaryFormatter()
  91.         {
  92.             m_surrogates = null;
  93.             m_context = new StreamingContext(StreamingContextStates.All);
  94.         }
  95.        
  96.         // Constructor
  97.         public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
  98.         {
  99.             m_surrogates = selector;
  100.             m_context = context;
  101.         }
  102.        
  103.        
  104.        
  105.         // Deserialize the stream into an object graph.
  106.         public object Deserialize(Stream serializationStream)
  107.         {
  108.             return Deserialize(serializationStream, null);
  109.         }
  110.        
  111.         internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck)
  112.         {
  113.             return Deserialize(serializationStream, null, fCheck, null);
  114.         }
  115.        
  116.        
  117.        
  118.         // Deserialize the stream into an object graph.
  119.         public object Deserialize(Stream serializationStream, HeaderHandler handler)
  120.         {
  121.             return Deserialize(serializationStream, handler, true, null);
  122.         }
  123.        
  124.         public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
  125.         {
  126.             return Deserialize(serializationStream, handler, true, methodCallMessage);
  127.         }
  128.        
  129.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter), System.Runtime.InteropServices.ComVisible(false)]
  130.         public object UnsafeDeserialize(Stream serializationStream, HeaderHandler handler)
  131.         {
  132.             return Deserialize(serializationStream, handler, false, null);
  133.         }
  134.        
  135.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter), System.Runtime.InteropServices.ComVisible(false)]
  136.         public object UnsafeDeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
  137.         {
  138.             return Deserialize(serializationStream, handler, false, methodCallMessage);
  139.         }
  140.        
  141.         internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck, IMethodCallMessage methodCallMessage)
  142.         {
  143.                 /*isCrossAppDomain*/            return Deserialize(serializationStream, handler, fCheck, false, methodCallMessage);
  144.         }
  145.        
  146.         // Deserialize the stream into an object graph.
  147.         internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck, bool isCrossAppDomain, IMethodCallMessage methodCallMessage)
  148.         {
  149.             if (serializationStream == null) {
  150.                 throw new ArgumentNullException("serializationStream", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentNull_WithParamName"), serializationStream));
  151.             }
  152.            
  153.             if (serializationStream.CanSeek && (serializationStream.Length == 0))
  154.                 throw new SerializationException(Environment.GetResourceString("Serialization_Stream"));
  155.            
  156.             SerTrace.Log(this, "Deserialize Entry");
  157.             InternalFE formatterEnums = new InternalFE();
  158.             formatterEnums.FEtypeFormat = m_typeFormat;
  159.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
  160.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  161.             formatterEnums.FEsecurityLevel = m_securityLevel;
  162.            
  163.             ObjectReader sor = new ObjectReader(serializationStream, m_surrogates, m_context, formatterEnums, m_binder);
  164.             sor.crossAppDomainArray = m_crossAppDomainArray;
  165.             return sor.Deserialize(handler, new __BinaryParser(serializationStream, sor), fCheck, isCrossAppDomain, methodCallMessage);
  166.         }
  167.        
  168.         public void Serialize(Stream serializationStream, object graph)
  169.         {
  170.             Serialize(serializationStream, graph, null);
  171.         }
  172.        
  173.         // Commences the process of serializing the entire graph. All of the data (in the appropriate format
  174.         // is emitted onto the stream).
  175.         public void Serialize(Stream serializationStream, object graph, Header[] headers)
  176.         {
  177.             Serialize(serializationStream, graph, headers, true);
  178.         }
  179.        
  180.         // Commences the process of serializing the entire graph. All of the data (in the appropriate format
  181.         // is emitted onto the stream).
  182.         internal void Serialize(Stream serializationStream, object graph, Header[] headers, bool fCheck)
  183.         {
  184.             if (serializationStream == null) {
  185.                 throw new ArgumentNullException("serializationStream", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentNull_WithParamName"), serializationStream));
  186.             }
  187.             SerTrace.Log(this, "Serialize Entry");
  188.            
  189.             InternalFE formatterEnums = new InternalFE();
  190.             formatterEnums.FEtypeFormat = m_typeFormat;
  191.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
  192.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  193.            
  194.             ObjectWriter sow = new ObjectWriter(m_surrogates, m_context, formatterEnums);
  195.             __BinaryWriter binaryWriter = new __BinaryWriter(serializationStream, sow, m_typeFormat);
  196.             sow.Serialize(graph, headers, binaryWriter, fCheck);
  197.             m_crossAppDomainArray = sow.crossAppDomainArray;
  198.         }
  199.        
  200.     }
  201. }

Developer Fusion