ProtoIncludeAttribute.cs 2.5 KB

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