BinaryCore.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. bool _includeVersions = true;
  20. bool _strictBinding = false;
  21. #if NET_1_1
  22. TypeFilterLevel _filterLevel = TypeFilterLevel.Low;
  23. #endif
  24. public static BinaryCore DefaultInstance = new BinaryCore ();
  25. public BinaryCore (object owner, IDictionary properties, string[] allowedProperties)
  26. {
  27. foreach(DictionaryEntry property in properties)
  28. {
  29. string key = (string) property.Key;
  30. if (Array.IndexOf (allowedProperties, key) == -1)
  31. throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
  32. switch (key)
  33. {
  34. case "includeVersions":
  35. _includeVersions = Convert.ToBoolean (property.Value);
  36. break;
  37. case "strictBinding":
  38. _strictBinding = Convert.ToBoolean (property.Value);
  39. break;
  40. #if NET_1_1
  41. case "typeFilterLevel":
  42. if (property.Value is TypeFilterLevel)
  43. _filterLevel = (TypeFilterLevel) property.Value;
  44. else {
  45. string s = (string) property.Value;
  46. _filterLevel = (TypeFilterLevel) Enum.Parse (typeof(TypeFilterLevel), s);
  47. }
  48. break;
  49. #endif
  50. }
  51. }
  52. Init ();
  53. }
  54. public BinaryCore ()
  55. {
  56. Init ();
  57. }
  58. public void Init ()
  59. {
  60. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  61. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  62. _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
  63. _deserializationFormatter = new BinaryFormatter (null, context);
  64. #if NET_1_1
  65. _serializationFormatter.FilterLevel = _filterLevel;
  66. _deserializationFormatter.FilterLevel = _filterLevel;
  67. #endif
  68. if (!_includeVersions)
  69. {
  70. _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  71. _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  72. }
  73. if (!_strictBinding)
  74. {
  75. _serializationFormatter.Binder = ChannelCore.SimpleBinder;
  76. _deserializationFormatter.Binder = ChannelCore.SimpleBinder;
  77. }
  78. }
  79. public BinaryFormatter Serializer
  80. {
  81. get { return _serializationFormatter; }
  82. }
  83. public BinaryFormatter Deserializer
  84. {
  85. get { return _deserializationFormatter; }
  86. }
  87. }
  88. }