Open sandboxFocus

Method IntroduceAttribute

IntroduceAttribute(IAdviser<IDeclaration>, IAttributeData, OverrideStrategy)

Adds a custom attribute to a given declaration. Use the With<TNewDeclaration>(TNewDeclaration) method to add the attribute to a different declaration than the current one.

Declaration
public static IIntroductionAdviceResult<IAttribute> IntroduceAttribute(this IAdviser<IDeclaration> adviser, IAttributeData attribute, OverrideStrategy whenExists = OverrideStrategy.Default)
Parameters
Type Name Description
IAdviser<IDeclaration> adviser

An adviser for a declaration.

IAttributeData attribute

The custom attribute to be added. It can be an existing IAttribute from the code model (as it implements IAttributeData), or you can use Create(Type, IReadOnlyList<object?>?, IReadOnlyList<KeyValuePair<string, object?>>?) to create a new attribute programmatically.

OverrideStrategy whenExists

Specifies the strategy to follow when an attribute of the same type already exists on the target declaration: Fail fails the compilation with an error (default); Ignore silently skips the introduction; Override removes all previous instances and replaces them with the new one; New adds the new instance regardless of existing attributes.

Returns
Type Description
IIntroductionAdviceResult<IAttribute>

An IIntroductionAdviceResult<T> exposing the introduced IAttribute.

See Also