CustomSerialization.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. /*
  35. * This entire source file is not supported on some platform
  36. */
  37. #if !NOSERIALIZABLE
  38. using System;
  39. using System.Security.Permissions;
  40. using System.Runtime.Serialization;
  41. using System.Security;
  42. namespace Google.ProtocolBuffers
  43. {
  44. /*
  45. * Specialized handing of *all* message types. Messages are serialized into a byte[] and stored
  46. * into the SerializationInfo, and are then reconstituted by an IObjectReference class after
  47. * deserialization. IDeserializationCallback is supported on both the Builder and Message.
  48. */
  49. [Serializable]
  50. partial class AbstractMessageLite<TMessage, TBuilder> : ISerializable
  51. {
  52. [SecurityCritical]
  53. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  54. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  55. {
  56. info.SetType(typeof(SerializationSurrogate));
  57. info.AddValue("message", ToByteArray());
  58. info.AddValue("initialized", IsInitialized);
  59. }
  60. [Serializable]
  61. private sealed class SerializationSurrogate : IObjectReference, ISerializable
  62. {
  63. static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
  64. private readonly byte[] _message;
  65. private readonly bool _initialized;
  66. private SerializationSurrogate(SerializationInfo info, StreamingContext context)
  67. {
  68. _message = (byte[])info.GetValue("message", typeof(byte[]));
  69. _initialized = info.GetBoolean("initialized");
  70. }
  71. [SecurityCritical]
  72. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  73. object IObjectReference.GetRealObject(StreamingContext context)
  74. {
  75. ExtensionRegistry registry = context.Context as ExtensionRegistry;
  76. TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
  77. builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
  78. IDeserializationCallback callback = builder as IDeserializationCallback;
  79. if(callback != null)
  80. {
  81. callback.OnDeserialization(context);
  82. }
  83. TMessage message = _initialized ? builder.Build() : builder.BuildPartial();
  84. callback = message as IDeserializationCallback;
  85. if (callback != null)
  86. {
  87. callback.OnDeserialization(context);
  88. }
  89. return message;
  90. }
  91. [SecurityCritical]
  92. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  93. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  94. {
  95. info.AddValue("message", _message);
  96. }
  97. }
  98. }
  99. [Serializable]
  100. partial class AbstractBuilderLite<TMessage, TBuilder> : ISerializable
  101. {
  102. [SecurityCritical]
  103. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  104. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  105. {
  106. info.SetType(typeof(SerializationSurrogate));
  107. info.AddValue("message", Clone().BuildPartial().ToByteArray());
  108. }
  109. [Serializable]
  110. private sealed class SerializationSurrogate : IObjectReference, ISerializable
  111. {
  112. static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
  113. private readonly byte[] _message;
  114. private SerializationSurrogate(SerializationInfo info, StreamingContext context)
  115. {
  116. _message = (byte[])info.GetValue("message", typeof(byte[]));
  117. }
  118. [SecurityCritical]
  119. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  120. object IObjectReference.GetRealObject(StreamingContext context)
  121. {
  122. ExtensionRegistry registry = context.Context as ExtensionRegistry;
  123. TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
  124. builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
  125. IDeserializationCallback callback = builder as IDeserializationCallback;
  126. if(callback != null)
  127. {
  128. callback.OnDeserialization(context);
  129. }
  130. return builder;
  131. }
  132. [SecurityCritical]
  133. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  134. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  135. {
  136. info.AddValue("message", _message);
  137. }
  138. }
  139. }
  140. /*
  141. * Spread some attribute love around, keeping this all here so we don't use conditional compliation
  142. * in every one of these classes. If we introduce a new platform that also does not support this
  143. * we can control it all from this source file.
  144. */
  145. [Serializable]
  146. partial class GeneratedMessageLite<TMessage, TBuilder> { }
  147. [Serializable]
  148. partial class ExtendableMessageLite<TMessage, TBuilder> { }
  149. [Serializable]
  150. partial class AbstractMessage<TMessage, TBuilder> { }
  151. [Serializable]
  152. partial class GeneratedMessage<TMessage, TBuilder> { }
  153. [Serializable]
  154. partial class ExtendableMessage<TMessage, TBuilder> { }
  155. [Serializable]
  156. partial class GeneratedBuilderLite<TMessage, TBuilder> { }
  157. [Serializable]
  158. partial class ExtendableBuilderLite<TMessage, TBuilder> { }
  159. [Serializable]
  160. partial class AbstractBuilder<TMessage, TBuilder> { }
  161. [Serializable]
  162. partial class GeneratedBuilder<TMessage, TBuilder> { }
  163. [Serializable]
  164. partial class ExtendableBuilder<TMessage, TBuilder> { }
  165. [Serializable]
  166. partial class DynamicMessage
  167. {
  168. [Serializable]
  169. partial class Builder { }
  170. }
  171. }
  172. #endif