Open sandboxFocus

Class TemplateAttribute

Marks a method, property, field, or event as a T# template that can be used to generate or transform code.

Inheritance
TemplateAttribute
Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event)]
public class TemplateAttribute : Attribute, ITemplateAttribute, IAdviceAttribute
Remarks

Templates are methods written in T#, a template language that is fully compatible with C# but has different semantics. T# combines compile-time logic with run-time code generation: templates execute at compile-time to produce the C# code that will run in your application. The T# compiler analyzes your code and separates compile-time portions from run-time portions, typically using the meta pseudo-keyword to identify compile-time expressions and statements.

This attribute marks a member as a template. When used with IntroduceAttribute for declarative member introduction, or with advising methods like IntroduceMethod(IAdviser<INamedType>, string, IntroductionScope, OverrideStrategy, Action<IMethodBuilder>?, object?, object?), the properties of this attribute (such as Name, Accessibility, IsVirtual) control the characteristics of the introduced member. When a property is not explicitly set, its value defaults to the corresponding characteristic of the template member itself.

Within templates, you can use meta.Target to access the declaration being overridden or introduced, and meta.Proceed() to invoke the original implementation when overriding. T# supports compile-time control flow (compile-time if and foreach) and dynamic typing through the dynamic keyword, which represents values whose types are unknown at template authoring time but are resolved when the template is applied to a specific target.

Constructors

Name Description
TemplateAttribute()

Properties

Name Description
Accessibility

Gets or sets the accessibility of the introduced member. When this property is not set, the accessibility is taken from the template member that this attribute is applied to.

Id

Gets or sets an identifier that can be used to reference this template from advising methods.

IsAbstract

Gets or sets a value indicating whether the introduced member is abstract. When this property is not set, the value is taken from the template member that this attribute is applied to.

IsEmpty

Gets or sets a value indicating whether this template is an empty placeholder implementation.

IsExtern

Gets or sets a value indicating whether the introduced member is extern. When this property is not set, the value is taken from the template member that this attribute is applied to.

IsPartial

Gets or sets a value indicating whether the introduced member is partial. When this property is not set, the value is taken from the template member that this attribute is applied to.

IsRequired

Gets or sets a value indicating whether the introduced member is required. When this property is not set, the value is taken from the template member that this attribute is applied to.

IsSealed

Gets or sets a value indicating whether the introduced member is sealed. When this property is not set, the value is taken from the template member that this attribute is applied to.

IsVirtual

Gets or sets a value indicating whether the introduced member is virtual. When this property is not set, the value is taken from the template member that this attribute is applied to.

Name

Gets or sets the name of the member introduced by this template.

Extension Methods

See Also