CustomSerialization.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Runtime.Serialization;
  40. using System.Security;
  41. namespace Google.ProtocolBuffers
  42. {
  43. /*
  44. * Specialized handing of *all* message types. Messages are serialized into a byte[] and stored
  45. * into the SerializationInfo, and are then reconstituted by an IObjectReference class after
  46. * deserialization. IDeserializationCallback is supported on both the Builder and Message.
  47. */
  48. [Serializable]
  49. partial class AbstractMessageLite<TMessage, TBuilder> : ISerializable
  50. {
  51. [SecurityCritical]
  52. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  53. {
  54. info.SetType(typeof(SerializationSurrogate));
  55. info.AddValue("message", ToByteArray());
  56. info.AddValue("initialized", IsInitialized);
  57. }
  58. [Serializable]
  59. private sealed class SerializationSurrogate : IObjectReference, ISerializable
  60. {
  61. static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
  62. private readonly byte[] _message;
  63. private readonly bool _initialized;
  64. private SerializationSurrogate(SerializationInfo info, StreamingContext context)
  65. {
  66. _message = (byte[])info.GetValue("message", typeof(byte[]));
  67. _initialized = info.GetBoolean("initialized");
  68. }
  69. object IObjectReference.GetRealObject(StreamingContext context)
  70. {
  71. ExtensionRegistry registry = context.Context as ExtensionRegistry;
  72. TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
  73. builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
  74. IDeserializationCallback callback = builder as IDeserializationCallback;
  75. if(callback != null)
  76. {
  77. callback.OnDeserialization(context);
  78. }
  79. TMessage message = _initialized ? builder.Build() : builder.BuildPartial();
  80. callback = message as IDeserializationCallback;
  81. if (callback != null)
  82. {
  83. callback.OnDeserialization(context);
  84. }
  85. return message;
  86. }
  87. [SecurityCritical]
  88. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  89. {
  90. info.AddValue("message", _message);
  91. }
  92. }
  93. }
  94. [Serializable]
  95. partial class AbstractBuilderLite<TMessage, TBuilder> : ISerializable
  96. {
  97. [SecurityCritical]
  98. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  99. {
  100. info.SetType(typeof(SerializationSurrogate));
  101. info.AddValue("message", Clone().BuildPartial().ToByteArray());
  102. }
  103. [Serializable]
  104. private sealed class SerializationSurrogate : IObjectReference, ISerializable
  105. {
  106. static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
  107. private readonly byte[] _message;
  108. private SerializationSurrogate(SerializationInfo info, StreamingContext context)
  109. {
  110. _message = (byte[])info.GetValue("message", typeof(byte[]));
  111. }
  112. object IObjectReference.GetRealObject(StreamingContext context)
  113. {
  114. ExtensionRegistry registry = context.Context as ExtensionRegistry;
  115. TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
  116. builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
  117. IDeserializationCallback callback = builder as IDeserializationCallback;
  118. if(callback != null)
  119. {
  120. callback.OnDeserialization(context);
  121. }
  122. return builder;
  123. }
  124. [SecurityCritical]
  125. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  126. {
  127. info.AddValue("message", _message);
  128. }
  129. }
  130. }
  131. /*
  132. * Spread some attribute love around, keeping this all here so we don't use conditional compliation
  133. * in every one of these classes. If we introduce a new platform that also does not support this
  134. * we can control it all from this source file.
  135. */
  136. [Serializable]
  137. partial class GeneratedMessageLite<TMessage, TBuilder> { }
  138. [Serializable]
  139. partial class ExtendableMessageLite<TMessage, TBuilder> { }
  140. [Serializable]
  141. partial class AbstractMessage<TMessage, TBuilder> { }
  142. [Serializable]
  143. partial class GeneratedMessage<TMessage, TBuilder> { }
  144. [Serializable]
  145. partial class ExtendableMessage<TMessage, TBuilder> { }
  146. [Serializable]
  147. partial class GeneratedBuilderLite<TMessage, TBuilder> { }
  148. [Serializable]
  149. partial class ExtendableBuilderLite<TMessage, TBuilder> { }
  150. [Serializable]
  151. partial class AbstractBuilder<TMessage, TBuilder> { }
  152. [Serializable]
  153. partial class GeneratedBuilder<TMessage, TBuilder> { }
  154. [Serializable]
  155. partial class ExtendableBuilder<TMessage, TBuilder> { }
  156. [Serializable]
  157. partial class DynamicMessage
  158. {
  159. [Serializable]
  160. partial class Builder { }
  161. }
  162. }
  163. #endif