using System; using System.ComponentModel; using ProtoBuf.Meta; namespace ProtoBuf { /// /// Indicates the known-types to support for an individual /// message. This serializes each level in the hierarchy as /// a nested message to retain wire-compatibility with /// other protocol-buffer implementations. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] public sealed class ProtoIncludeAttribute : Attribute { /// /// Creates a new instance of the ProtoIncludeAttribute. /// /// The unique index (within the type) that will identify this data. /// The additional type to serialize/deserialize. public ProtoIncludeAttribute(int tag, Type knownType) : this(tag, knownType == null ? "" : knownType.AssemblyQualifiedName) { } /// /// Creates a new instance of the ProtoIncludeAttribute. /// /// The unique index (within the type) that will identify this data. /// The additional type to serialize/deserialize. public ProtoIncludeAttribute(int tag, string knownTypeName) { if (tag <= 0) throw new ArgumentOutOfRangeException(nameof(tag), "Tags must be positive integers"); if (string.IsNullOrEmpty(knownTypeName)) throw new ArgumentNullException(nameof(knownTypeName), "Known type cannot be blank"); Tag = tag; KnownTypeName = knownTypeName; } /// /// Gets the unique index (within the type) that will identify this data. /// public int Tag { get; } /// /// Gets the additional type to serialize/deserialize. /// public string KnownTypeName { get; } /// /// Gets the additional type to serialize/deserialize. /// public Type KnownType => TypeModel.ResolveKnownType(KnownTypeName, null, null); /// /// Specifies whether the inherited sype's sub-message should be /// written with a length-prefix (default), or with group markers. /// [DefaultValue(DataFormat.Default)] public DataFormat DataFormat { get; set; } = DataFormat.Default; } }