Guest User

Untitled

a guest
Aug 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. protobuf-net Serializing as base class
  2. [ProtoContract]
  3. class Base
  4. {
  5. [ProtoMember(1)]
  6. public string PublicInfo { get; set; }
  7. }
  8.  
  9. class Derived : Base
  10. {
  11. public string SecretInfo { get; set; }
  12. }
  13.  
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. Derived d = new Derived()
  19. {
  20. PublicInfo = "public info",
  21. SecretInfo = "secret info"
  22. };
  23. using (var ms = new MemoryStream())
  24. {
  25. Serializer.NonGeneric.Serialize(ms, d as Base);
  26. ms.Seek(0, SeekOrigin.Begin);
  27. Base deserialized = Serializer.Deserialize<Base>(ms);
  28. Console.WriteLine("Deserialized type: " + deserialized.GetType());
  29. Console.WriteLine("Deserialized value: " + deserialized.PublicInfo);
  30. }
  31. Console.ReadLine();
  32. }
  33. }
  34.  
  35. Deserialized type: Base
  36. Deserialized value: public info
  37.  
  38. [ProtoContract] class Generic {
  39. [ProtoMember(1)] public string PublicInfo { get; set; }
  40. }
  41.  
  42. class Specialized {
  43. public Generic Generic { get; set; }
  44. public string SecretInfo { get; set; }
  45. }
  46.  
  47. namespace NHibernate.Proxy {
  48. internal interface INHibernateProxy {} // pretty spectacularly evil
  49. }
  50. ...
  51. class Derived : Base, INHibernateProxy {}
Add Comment
Please, Sign In to add comment