mscorlib(4.0.0.0) API with additions
BinaryFormatter.cs
2 using System.IO;
5 using System.Security;
6 
8 {
10  [ComVisible(true)]
12  {
13  internal ISurrogateSelector m_surrogates;
14 
15  internal StreamingContext m_context;
16 
17  internal SerializationBinder m_binder;
18 
19  internal FormatterTypeStyle m_typeFormat = FormatterTypeStyle.TypesAlways;
20 
21  internal FormatterAssemblyStyle m_assemblyFormat;
22 
23  internal TypeFilterLevel m_securityLevel = TypeFilterLevel.Full;
24 
25  internal object[] m_crossAppDomainArray;
26 
27  private static Dictionary<Type, TypeInformation> typeNameCache = new Dictionary<Type, TypeInformation>();
28 
32  {
33  get
34  {
35  return m_typeFormat;
36  }
37  set
38  {
39  m_typeFormat = value;
40  }
41  }
42 
46  {
47  get
48  {
49  return m_assemblyFormat;
50  }
51  set
52  {
53  m_assemblyFormat = value;
54  }
55  }
56 
60  {
61  get
62  {
63  return m_securityLevel;
64  }
65  set
66  {
67  m_securityLevel = value;
68  }
69  }
70 
74  {
75  get
76  {
77  return m_surrogates;
78  }
79  set
80  {
81  m_surrogates = value;
82  }
83  }
84 
88  {
89  get
90  {
91  return m_binder;
92  }
93  set
94  {
95  m_binder = value;
96  }
97  }
98 
102  {
103  get
104  {
105  return m_context;
106  }
107  set
108  {
109  m_context = value;
110  }
111  }
112 
115  {
116  m_surrogates = null;
117  m_context = new StreamingContext(StreamingContextStates.All);
118  }
119 
124  {
125  m_surrogates = selector;
126  m_context = context;
127  }
128 
136  public object Deserialize(Stream serializationStream)
137  {
138  return Deserialize(serializationStream, null);
139  }
140 
141  [SecurityCritical]
142  internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck)
143  {
144  return Deserialize(serializationStream, handler, fCheck, null);
145  }
146 
155  [SecuritySafeCritical]
156  public object Deserialize(Stream serializationStream, HeaderHandler handler)
157  {
158  return Deserialize(serializationStream, handler, fCheck: true);
159  }
160 
169  [SecuritySafeCritical]
170  public object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
171  {
172  return Deserialize(serializationStream, handler, fCheck: true, methodCallMessage);
173  }
174 
182  [SecurityCritical]
183  [ComVisible(false)]
184  public object UnsafeDeserialize(Stream serializationStream, HeaderHandler handler)
185  {
186  return Deserialize(serializationStream, handler, fCheck: false);
187  }
188 
197  [SecurityCritical]
198  [ComVisible(false)]
199  public object UnsafeDeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
200  {
201  return Deserialize(serializationStream, handler, fCheck: false, methodCallMessage);
202  }
203 
204  [SecurityCritical]
205  internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck, IMethodCallMessage methodCallMessage)
206  {
207  return Deserialize(serializationStream, handler, fCheck, isCrossAppDomain: false, methodCallMessage);
208  }
209 
210  [SecurityCritical]
211  internal object Deserialize(Stream serializationStream, HeaderHandler handler, bool fCheck, bool isCrossAppDomain, IMethodCallMessage methodCallMessage)
212  {
213  if (serializationStream == null)
214  {
215  throw new ArgumentNullException("serializationStream", Environment.GetResourceString("ArgumentNull_WithParamName", serializationStream));
216  }
217  if (serializationStream.CanSeek && serializationStream.Length == 0L)
218  {
219  throw new SerializationException(Environment.GetResourceString("Serialization_Stream"));
220  }
221  InternalFE internalFE = new InternalFE();
222  internalFE.FEtypeFormat = m_typeFormat;
223  internalFE.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
224  internalFE.FEassemblyFormat = m_assemblyFormat;
225  internalFE.FEsecurityLevel = m_securityLevel;
226  ObjectReader objectReader = new ObjectReader(serializationStream, m_surrogates, m_context, internalFE, m_binder);
227  objectReader.crossAppDomainArray = m_crossAppDomainArray;
228  return objectReader.Deserialize(handler, new __BinaryParser(serializationStream, objectReader), fCheck, isCrossAppDomain, methodCallMessage);
229  }
230 
237  public void Serialize(Stream serializationStream, object graph)
238  {
239  Serialize(serializationStream, graph, null);
240  }
241 
249  [SecuritySafeCritical]
250  public void Serialize(Stream serializationStream, object graph, Header[] headers)
251  {
252  Serialize(serializationStream, graph, headers, fCheck: true);
253  }
254 
255  [SecurityCritical]
256  internal void Serialize(Stream serializationStream, object graph, Header[] headers, bool fCheck)
257  {
258  if (serializationStream == null)
259  {
260  throw new ArgumentNullException("serializationStream", Environment.GetResourceString("ArgumentNull_WithParamName", serializationStream));
261  }
262  InternalFE internalFE = new InternalFE();
263  internalFE.FEtypeFormat = m_typeFormat;
264  internalFE.FEserializerTypeEnum = InternalSerializerTypeE.Binary;
265  internalFE.FEassemblyFormat = m_assemblyFormat;
266  ObjectWriter objectWriter = new ObjectWriter(m_surrogates, m_context, internalFE, m_binder);
267  __BinaryWriter serWriter = new __BinaryWriter(serializationStream, objectWriter, m_typeFormat);
268  objectWriter.Serialize(graph, headers, serWriter, fCheck);
269  m_crossAppDomainArray = objectWriter.crossAppDomainArray;
270  }
271 
272  internal static TypeInformation GetTypeInformation(Type type)
273  {
274  lock (typeNameCache)
275  {
276  TypeInformation value = null;
277  if (!typeNameCache.TryGetValue(type, out value))
278  {
279  bool hasTypeForwardedFrom;
280  string clrAssemblyName = FormatterServices.GetClrAssemblyName(type, out hasTypeForwardedFrom);
281  value = new TypeInformation(FormatterServices.GetClrTypeFullName(type), clrAssemblyName, hasTypeForwardedFrom);
282  typeNameCache.Add(type, value);
283  }
284  return value;
285  }
286  }
287  }
288 }
object UnsafeDeserialize(Stream serializationStream, HeaderHandler handler)
Deserializes the specified stream into an object graph. The provided T:System.Runtime....
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
TypeFilterLevel
Specifies the level of automatic deserialization for .NET Framework remoting.
Allows users to control class loading and mandate what class to load.
abstract bool CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeki...
Definition: Stream.cs:587
Defines the method call message interface.
object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
Deserializes a response to a remote method call from the provided T:System.IO.Stream.
FormatterTypeStyle TypeFormat
Gets or sets the format in which type descriptions are laid out in the serialized stream.
void Serialize(Stream serializationStream, object graph)
Serializes the object, or graph of objects with the specified top (root), to the given stream.
Definition: __Canon.cs:3
TypeFilterLevel FilterLevel
Gets or sets the T:System.Runtime.Serialization.Formatters.TypeFilterLevel of automatic deserializati...
FormatterAssemblyStyle AssemblyFormat
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
object Deserialize(Stream serializationStream)
Deserializes the specified stream into an object graph.
FormatterAssemblyStyle
Indicates the method that will be used during deserialization for locating and loading assemblies.
StreamingContext Context
Gets or sets the T:System.Runtime.Serialization.StreamingContext for this formatter.
Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
FormatterTypeStyle
Indicates the format in which type descriptions are laid out in the serialized stream.
object UnsafeDeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
Deserializes a response to a remote method call from the provided T:System.IO.Stream.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Provides the remote procedure call (RPC) interface for all formatters.
Provides functionality for formatting serialized objects.
Definition: IFormatter.cs:8
SerializationBinder Binder
Gets or sets an object of type T:System.Runtime.Serialization.SerializationBinder that controls the b...
object Deserialize(Stream serializationStream, HeaderHandler handler)
Deserializes the specified stream into an object graph. The provided T:System.Runtime....
Defines the out-of-band data for a call.
Definition: Header.cs:8
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
The exception thrown when an error occurs during serialization or deserialization.
Represents a collection of keys and values.To browse the .NET Framework source code for this type,...
Definition: Dictionary.cs:17
Indicates a serialization surrogate selector class.
abstract long Length
When overridden in a derived class, gets the length in bytes of the stream.
Definition: Stream.cs:621
BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
Initializes a new instance of the T:System.Runtime.Serialization.Formatters.Binary....
void Serialize(Stream serializationStream, object graph, Header[] headers)
Serializes the object, or graph of objects with the specified top (root), to the given stream attachi...
BinaryFormatter()
Initializes a new instance of the T:System.Runtime.Serialization.Formatters.Binary....
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
Provides static methods to aid with the implementation of a T:System.Runtime.Serialization....
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
Assists formatters in selection of the serialization surrogate to delegate the serialization or deser...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16