BinaryCore.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // System.Runtime.Remoting.Channels.BinaryCore.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2003 (C) Copyright, Novell, Inc.
  7. //
  8. using System.Collections;
  9. using System.Runtime.Remoting.Messaging;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.Serialization.Formatters;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. namespace System.Runtime.Remoting.Channels
  14. {
  15. internal class BinaryCore
  16. {
  17. BinaryFormatter _serializationFormatter;
  18. BinaryFormatter _deserializationFormatter;
  19. public static BinaryCore DefaultInstance = new BinaryCore (true, false);
  20. public BinaryCore (object owner, IDictionary properties)
  21. {
  22. bool includeVersions = true;
  23. bool strictBinding = false;
  24. foreach(DictionaryEntry property in properties)
  25. {
  26. switch((string)property.Key)
  27. {
  28. case "includeVersions":
  29. includeVersions = Convert.ToBoolean (property.Value);
  30. break;
  31. case "strictBinding":
  32. strictBinding = Convert.ToBoolean (property.Value);
  33. break;
  34. default:
  35. throw new RemotingException (owner.GetType().Name + " does not recognize '" + property.Key + "' configuration property");
  36. }
  37. }
  38. Init (includeVersions, strictBinding);
  39. }
  40. public BinaryCore (bool includeVersions, bool stringBinding)
  41. {
  42. Init (includeVersions, stringBinding);
  43. }
  44. public void Init (bool includeVersions, bool stringBinding)
  45. {
  46. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  47. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  48. _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
  49. _deserializationFormatter = new BinaryFormatter (null, context);
  50. if (!includeVersions)
  51. {
  52. _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  53. _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  54. }
  55. if (!stringBinding)
  56. {
  57. _serializationFormatter.Binder = ChannelCore.SimpleBinder;
  58. _deserializationFormatter.Binder = ChannelCore.SimpleBinder;
  59. }
  60. }
  61. public BinaryFormatter Serializer
  62. {
  63. get { return _serializationFormatter; }
  64. }
  65. public BinaryFormatter Deserializer
  66. {
  67. get { return _deserializationFormatter; }
  68. }
  69. }
  70. }