Skip to content

ValidAttribute

A validated attribute with guaranteed non-null AttributeData.

See also: Projections Overview | OptionalAttribute | AttributeQuery

Getting Arguments

Same argument extraction methods as OptionalAttribute:

OptionalArgument<string> arg = validAttr.ConstructorArg<string>(0);
OptionalArgument<int> retries = validAttr.NamedArg<int>("MaxRetries");
validAttr.GetNamedArgument<bool>("Enabled");  // alternate syntax

Direct Access

validAttr.Value             // AttributeData
validAttr.AttributeClass    // INamedTypeSymbol

Generic Attribute Type Arguments

For generic attributes, type arguments return ValidSymbol:

ImmutableArray<ValidSymbol<INamedTypeSymbol>> typeArgs = validAttr.GetTypeArguments();

Type Checking

Check or narrow the attribute type:

// Check if this attribute is a specific type
bool isRetry = validAttr.Is<RetryAttribute>();

// Narrow to OptionalAttribute if it matches (empty if not)
OptionalAttribute optional = validAttr.As<RetryAttribute>();

Converting to Query Types

Use AsQuery<TQuery>() to convert to a strongly-typed query wrapper:

// Convert to a domain-specific query type
var query = validAttr.AsQuery<MyAttributeQuery>();

// Access typed properties with defaults
var maxRetries = query.MaxRetries;  // int, not Optional

See AttributeQuery for details on creating query types.