ProtoContractAttribute.cs 6.5 KB

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