ValueTagAttribute Class

Tags a value so the Meziantou.Framework.TaggedValues analyzer can report when values with different tags are mixed, for instance when an order id is compared with a project id.

[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
public sealed class ValueTagAttribute : Attribute

Remarks

On a field, a property, a parameter, or a return value ([return: ValueTag("OrderId")]), the tags describe the value. When the declared type is a collection, a Nullable<T>, a Task<TResult> or a ValueTask<TResult>, the tags describe the element or the result. Use ValueTagAttribute.Key and ValueTagAttribute.Value for dictionaries.

Several tags declare a union: the value is compatible with any value that shares at least one tag.

On the assembly, ValueTagAttribute tags a property or a field of a type you do not own, for instance [assembly: ValueTag(typeof(Process), nameof(Process.Id), "ProcessId")].

Local variables cannot have attributes. Use a comment instead: Guid /* ValueTag=OrderId */ id = ...;.

Inheritance
Attribute ValueTagAttribute

Constructors

NameDescription
ValueTagAttribute(string[])Initializes a new instance of the ValueTagAttribute class.
ValueTagAttribute(Type, string, string[])Initializes a new instance of the ValueTagAttribute class that tags a member of another type.

ValueTagAttribute overloads

ValueTagAttribute(string[])

ValueTagAttribute.cs

Initializes a new instance of the ValueTagAttribute class.

public ValueTagAttribute(params string[] tags)
Parameters
tags string[]
The tags of the value. Several tags declare a union.

ValueTagAttribute(Type, string, string[])

ValueTagAttribute.cs

Initializes a new instance of the ValueTagAttribute class that tags a member of another type.

public ValueTagAttribute(Type type, string memberName, params string[] tags)
Parameters
type Type
The type that declares the member. The tags also apply to the member when it is accessed through a derived type.
memberName string
The name of the property or the field to tag.
tags string[]
The tags of the value. Several tags declare a union.

Properties

NameDescription
KeyGets or sets the tag of the keys of a dictionary.
MemberNameGets the name of the tagged member, when the attribute is applied to the assembly.
TagsGets the tags of the value.
TypeGets the type that declares the tagged member, when the attribute is applied to the assembly.
ValueGets or sets the tag of the values of a dictionary.

Gets or sets the tag of the keys of a dictionary.

public string? Key { get; set; }

Property value

string?

Gets the name of the tagged member, when the attribute is applied to the assembly.

public string? MemberName { get; }

Property value

string?

Gets the tags of the value.

public IReadOnlyList<string> Tags { get; }

Property value

IReadOnlyList<string>

Gets the type that declares the tagged member, when the attribute is applied to the assembly.

public Type? Type { get; }

Property value

Type?

Gets or sets the tag of the values of a dictionary.

public string? Value { get; set; }

Property value

string?