ProtoContractAttribute.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. namespace ProtoBuf
  3. {
  4. /// <summary>
  5. /// Indicates that a type is defined for protocol-buffer serialization.
  6. /// </summary>
  7. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface,
  8. AllowMultiple = false, Inherited = false)]
  9. public sealed class ProtoContractAttribute : Attribute
  10. {
  11. /// <summary>
  12. /// Gets or sets the defined name of the type.
  13. /// </summary>
  14. public string Name { get { return name; } set { name = value; } }
  15. private string name;
  16. /// <summary>
  17. /// Gets or sets the fist offset to use with implicit field tags;
  18. /// only uesd if ImplicitFields is set.
  19. /// </summary>
  20. public int ImplicitFirstTag
  21. {
  22. get { return implicitFirstTag; }
  23. set
  24. {
  25. if (value < 1) throw new ArgumentOutOfRangeException("ImplicitFirstTag");
  26. implicitFirstTag = value;
  27. }
  28. }
  29. private int implicitFirstTag;
  30. /// <summary>
  31. /// If specified, alternative contract markers (such as markers for XmlSerailizer or DataContractSerializer) are ignored.
  32. /// </summary>
  33. public bool UseProtoMembersOnly
  34. {
  35. get { return HasFlag(OPTIONS_UseProtoMembersOnly); }
  36. set { SetFlag(OPTIONS_UseProtoMembersOnly, value); }
  37. }
  38. /// <summary>
  39. /// If specified, do NOT treat this type as a list, even if it looks like one.
  40. /// </summary>
  41. public bool IgnoreListHandling
  42. {
  43. get { return HasFlag(OPTIONS_IgnoreListHandling); }
  44. set { SetFlag(OPTIONS_IgnoreListHandling, value); }
  45. }
  46. /// <summary>
  47. /// Gets or sets the mechanism used to automatically infer field tags
  48. /// for members. This option should be used in advanced scenarios only.
  49. /// Please review the important notes against the ImplicitFields enumeration.
  50. /// </summary>
  51. public ImplicitFields ImplicitFields { get { return implicitFields; } set { implicitFields = value; } }
  52. private ImplicitFields implicitFields;
  53. /// <summary>
  54. /// Enables/disables automatic tag generation based on the existing name / order
  55. /// of the defined members. This option is not used for members marked
  56. /// with ProtoMemberAttribute, as intended to provide compatibility with
  57. /// WCF serialization. WARNING: when adding new fields you must take
  58. /// care to increase the Order for new elements, otherwise data corruption
  59. /// may occur.
  60. /// </summary>
  61. /// <remarks>If not explicitly specified, the default is assumed from Serializer.GlobalOptions.InferTagFromName.</remarks>
  62. public bool InferTagFromName
  63. {
  64. get { return HasFlag(OPTIONS_InferTagFromName); }
  65. set {
  66. SetFlag(OPTIONS_InferTagFromName, value);
  67. SetFlag(OPTIONS_InferTagFromNameHasValue, true);
  68. }
  69. }
  70. /// <summary>
  71. /// Has a InferTagFromName value been explicitly set? if not, the default from the type-model is assumed.
  72. /// </summary>
  73. internal bool InferTagFromNameHasValue
  74. { // note that this property is accessed via reflection and should not be removed
  75. get { return HasFlag(OPTIONS_InferTagFromNameHasValue); }
  76. }
  77. private int dataMemberOffset;
  78. /// <summary>
  79. /// Specifies an offset to apply to [DataMember(Order=...)] markers;
  80. /// this is useful when working with mex-generated classes that have
  81. /// a different origin (usually 1 vs 0) than the original data-contract.
  82. ///
  83. /// This value is added to the Order of each member.
  84. /// </summary>
  85. public int DataMemberOffset
  86. {
  87. get { return dataMemberOffset; }
  88. set { dataMemberOffset = value; }
  89. }
  90. /// <summary>
  91. /// If true, the constructor for the type is bypassed during deserialization, meaning any field initializers
  92. /// or other initialization code is skipped.
  93. /// </summary>
  94. public bool SkipConstructor
  95. {
  96. get { return HasFlag(OPTIONS_SkipConstructor); }
  97. set { SetFlag(OPTIONS_SkipConstructor, value); }
  98. }
  99. /// <summary>
  100. /// Should this type be treated as a reference by default? Please also see the implications of this,
  101. /// as recorded on ProtoMemberAttribute.AsReference
  102. /// </summary>
  103. public bool AsReferenceDefault
  104. {
  105. get { return HasFlag(OPTIONS_AsReferenceDefault); }
  106. set {
  107. SetFlag(OPTIONS_AsReferenceDefault, value);
  108. }
  109. }
  110. private bool HasFlag(byte flag) { return (flags & flag) == flag; }
  111. private void SetFlag(byte flag, bool value)
  112. {
  113. if (value) flags |= flag;
  114. else flags = (byte)(flags & ~flag);
  115. }
  116. private byte flags;
  117. private const byte
  118. OPTIONS_InferTagFromName = 1,
  119. OPTIONS_InferTagFromNameHasValue = 2,
  120. OPTIONS_UseProtoMembersOnly = 4,
  121. OPTIONS_SkipConstructor = 8,
  122. OPTIONS_IgnoreListHandling = 16,
  123. OPTIONS_AsReferenceDefault = 32,
  124. OPTIONS_EnumPassthru = 64,
  125. OPTIONS_EnumPassthruHasValue = 128;
  126. /// <summary>
  127. /// Applies only to enums (not to DTO classes themselves); gets or sets a value indicating that an enum should be treated directly as an int/short/etc, rather
  128. /// than enforcing .proto enum rules. This is useful *in particul* for [Flags] enums.
  129. /// </summary>
  130. public bool EnumPassthru
  131. {
  132. get { return HasFlag(OPTIONS_EnumPassthru); }
  133. set {
  134. SetFlag(OPTIONS_EnumPassthru, value);
  135. SetFlag(OPTIONS_EnumPassthruHasValue, true);
  136. }
  137. }
  138. /// <summary>
  139. /// Has a EnumPassthru value been explicitly set?
  140. /// </summary>
  141. internal bool EnumPassthruHasValue
  142. { // note that this property is accessed via reflection and should not be removed
  143. get { return HasFlag(OPTIONS_EnumPassthruHasValue); }
  144. }
  145. }
  146. }