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