BinaryCore.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.Runtime.Remoting.Messaging;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.Serialization.Formatters;
  32. using System.Runtime.Serialization.Formatters.Binary;
  33. namespace System.Runtime.Remoting.Channels
  34. {
  35. internal class BinaryCore
  36. {
  37. BinaryFormatter _serializationFormatter;
  38. BinaryFormatter _deserializationFormatter;
  39. bool _includeVersions = true;
  40. bool _strictBinding = false;
  41. IDictionary _properties;
  42. #if NET_1_1
  43. TypeFilterLevel _filterLevel = TypeFilterLevel.Low;
  44. #endif
  45. public static BinaryCore DefaultInstance = new BinaryCore ();
  46. public BinaryCore (object owner, IDictionary properties, string[] allowedProperties)
  47. {
  48. _properties = properties;
  49. foreach(DictionaryEntry property in properties)
  50. {
  51. string key = (string) property.Key;
  52. if (Array.IndexOf (allowedProperties, key) == -1)
  53. throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
  54. switch (key)
  55. {
  56. case "includeVersions":
  57. _includeVersions = Convert.ToBoolean (property.Value);
  58. break;
  59. case "strictBinding":
  60. _strictBinding = Convert.ToBoolean (property.Value);
  61. break;
  62. #if NET_1_1
  63. case "typeFilterLevel":
  64. if (property.Value is TypeFilterLevel)
  65. _filterLevel = (TypeFilterLevel) property.Value;
  66. else {
  67. string s = (string) property.Value;
  68. _filterLevel = (TypeFilterLevel) Enum.Parse (typeof(TypeFilterLevel), s);
  69. }
  70. break;
  71. #endif
  72. }
  73. }
  74. Init ();
  75. }
  76. public BinaryCore ()
  77. {
  78. _properties = new Hashtable ();
  79. Init ();
  80. }
  81. public void Init ()
  82. {
  83. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  84. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  85. _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
  86. _deserializationFormatter = new BinaryFormatter (null, context);
  87. #if NET_1_1
  88. _serializationFormatter.FilterLevel = _filterLevel;
  89. _deserializationFormatter.FilterLevel = _filterLevel;
  90. #endif
  91. if (!_includeVersions)
  92. {
  93. _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  94. _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  95. }
  96. if (!_strictBinding)
  97. {
  98. _serializationFormatter.Binder = ChannelCore.SimpleBinder;
  99. _deserializationFormatter.Binder = ChannelCore.SimpleBinder;
  100. }
  101. }
  102. public BinaryFormatter Serializer
  103. {
  104. get { return _serializationFormatter; }
  105. }
  106. public BinaryFormatter Deserializer
  107. {
  108. get { return _deserializationFormatter; }
  109. }
  110. public IDictionary Properties
  111. {
  112. get { return _properties; }
  113. }
  114. #if NET_1_1
  115. public TypeFilterLevel TypeFilterLevel
  116. {
  117. get { return _filterLevel; }
  118. }
  119. #endif
  120. }
  121. }