ProtoIncludeAttribute.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.ComponentModel;
  3. using ProtoBuf.Meta;
  4. #if FEAT_IKVM
  5. using Type = IKVM.Reflection.Type;
  6. using IKVM.Reflection;
  7. #else
  8. #endif
  9. namespace ProtoBuf
  10. {
  11. /// <summary>
  12. /// Indicates the known-types to support for an individual
  13. /// message. This serializes each level in the hierarchy as
  14. /// a nested message to retain wire-compatibility with
  15. /// other protocol-buffer implementations.
  16. /// </summary>
  17. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)]
  18. public sealed class ProtoIncludeAttribute : Attribute
  19. {
  20. ///<summary>
  21. /// Creates a new instance of the ProtoIncludeAttribute.
  22. /// </summary>
  23. /// <param name="tag">The unique index (within the type) that will identify this data.</param>
  24. /// <param name="knownType">The additional type to serialize/deserialize.</param>
  25. public ProtoIncludeAttribute(int tag, System.Type knownType)
  26. : this(tag, knownType == null ? "" : knownType.AssemblyQualifiedName) { }
  27. /// <summary>
  28. /// Creates a new instance of the ProtoIncludeAttribute.
  29. /// </summary>
  30. /// <param name="tag">The unique index (within the type) that will identify this data.</param>
  31. /// <param name="knownTypeName">The additional type to serialize/deserialize.</param>
  32. public ProtoIncludeAttribute(int tag, string knownTypeName)
  33. {
  34. if (tag <= 0) throw new ArgumentOutOfRangeException("tag", "Tags must be positive integers");
  35. if (Helpers.IsNullOrEmpty(knownTypeName)) throw new ArgumentNullException("knownTypeName", "Known type cannot be blank");
  36. this.tag = tag;
  37. this.knownTypeName = knownTypeName;
  38. }
  39. /// <summary>
  40. /// Gets the unique index (within the type) that will identify this data.
  41. /// </summary>
  42. public int Tag { get { return tag; } }
  43. private readonly int tag;
  44. /// <summary>
  45. /// Gets the additional type to serialize/deserialize.
  46. /// </summary>
  47. public string KnownTypeName { get { return knownTypeName; } }
  48. private readonly string knownTypeName;
  49. /// <summary>
  50. /// Gets the additional type to serialize/deserialize.
  51. /// </summary>
  52. public Type KnownType
  53. {
  54. get
  55. {
  56. return TypeModel.ResolveKnownType(KnownTypeName, null, null);
  57. }
  58. }
  59. /// <summary>
  60. /// Specifies whether the inherited sype's sub-message should be
  61. /// written with a length-prefix (default), or with group markers.
  62. /// </summary>
  63. [DefaultValue(DataFormat.Default)]
  64. public DataFormat DataFormat
  65. {
  66. get { return dataFormat; }
  67. set { dataFormat = value; }
  68. }
  69. private DataFormat dataFormat = DataFormat.Default;
  70. }
  71. }