BinaryCore.cs 3.0 KB

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