Open sandboxFocus

Class meta

The entry point for the T# template language, providing compile-time APIs to inspect and transform target code.

Inheritance
meta
Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public static class meta
Remarks

The meta class is a pseudo-keyword that serves as the gateway to Metalama's meta-programming capabilities. It can only be used within the context of a T# template method (methods marked with TemplateAttribute).

Key capabilities provided by meta:

In T# templates, expressions beginning with meta. are evaluated at compile-time to generate run-time code. The T# compiler separates compile-time portions from run-time portions using inference rules, with meta serving as the marker for compile-time expressions.

Properties

Name Description
AspectInstance

Gets the current IAspectInstance, which gives access to the Predecessors and the SecondaryInstances of the current aspect.

Base

Gets a dynamic object that gives access to the members of the base class for the current instance, equivalent to base in C#. The syntax to access these members is e.g. meta.Base.MyMethod().

BaseType

Gets a dynamic object that must be used to get access to static members of the type (e.g. meta.BaseStatic.MyStaticMethod()). If the current declaration is an extension member, the property returns the declaring type, not the receiver type.

Receiver

Gets a dynamic object that represents the receiver object, i.e. this in non-extension instance members, or the receiver parameter in extension members or classic extension methods.

Tags

Gets the dictionary of tags that were passed to the IAdviceFactory method by the BuildAspect(IAspectBuilder<T>) method.

Target

Gets access to the declaration being overridden or introduced.

This

Gets a dynamic object that represents the current instance (this) of the object being advised. It can be used as a value (e.g. as a method argument) or can be used to get access to instance members of the instance (e.g. meta.This.MyMethod()).

ThisType

Gets a dynamic object that must be used to get access to static members of the type (e.g. meta.ThisStatic.MyStaticMethod()). If the current declaration is an extension member, the property returns the declaring type, not the receiver type.

Methods

Name Description
Cast(IType, dynamic?)

Generates the cast syntax for the specified type.

CompileTime<T>(T?)

Coerces an expression to be interpreted as compile time. This is typically used to coerce expressions that can be either run-time or compile-time, such as a literal. Since ambiguous expressions are interpreted as run-time by default, this method allows to change that behavior.

DebugBreak()

Requests the debugger to break, if any debugger is attached to the current process.

Default(IType)

Generates the default(T) syntax for the specified type.

Default(SpecialType)

Generates the default(T) syntax for the specified type.

Default(Type)

Generates the default(T) syntax for the specified type.

DefineLocalVariable(string, IExpression)

Programmatically defines a local variable in run-time code with a type inferred from the initial value expression.

DefineLocalVariable(string, IType)

Programmatically defines a local variable in run-time code with a specified type but no initial value.

DefineLocalVariable(string, IType, IExpression?)

Programmatically defines a local variable in run-time code with a specified type and initial value expression.

DefineLocalVariable(string, IType, dynamic)

Programmatically defines a local variable in run-time code with a specified type and initial value.

DefineLocalVariable(string, dynamic)

Programmatically defines a local variable in run-time code with a type inferred from the initial value.

DefineLocalVariable(string, Type)

Programmatically defines a local variable in run-time code with a specified reflection type but no initial value.

DefineLocalVariable(string, Type, IExpression?)

Programmatically defines a local variable in run-time code with a specified reflection type and initial value expression.

DefineLocalVariable(string, Type, dynamic)

Programmatically defines a local variable in run-time code with a specified reflection type and initial value.

InsertComment(params string?[])

Injects a comment to the target code.

InsertStatement(IExpression)

Inserts a statement into the target code, where the statement is given as an IExpression. Note that not all expressions can be used as statements.

InsertStatement(IStatement)

Inserts a statement into the target code, where the statement is given as an IStatement.

InsertStatement(string)

Inserts a statement into the target code, where the statement is given as a string. Calling this overload is equivalent to calling the InsertStatement(IStatement) overload with the result of the Parse(string) method.

InvokeTemplate(TemplateInvocation, object?)

Calls another template method.

InvokeTemplate(string, ITemplateProvider?, object?)

Calls another template method. This overload accepts an ITemplateProvider.

InvokeTemplate(string, TemplateProvider, object?)

Calls another template method. This overload accepts a TemplateProvider.

Proceed()

Invokes the logic that has been overwritten. For instance, in an OverrideMethodAspect, calling Proceed() invokes the method being overridden. Note that the way how the logic is invoked (as a method call or inlining) is considered an implementation detail.

ProceedAsync()

Synonym to Proceed(), but the return type is exposed as a Task<dynamic?>. Only use this method when the return type of the method or accessor is task-like. Note that the actual return type of the overridden method or accessor is the one of the overwritten semantic, so it can be a void Task, a ValueType, or any other type.

ProceedAsyncEnumerable()
ProceedAsyncEnumerator()
ProceedEnumerable()

Synonym to Proceed(), but the return type is exposed as a IEnumerable<dynamic?>.

ProceedEnumerator()

Synonym to Proceed(), but the return type is exposed as a IEnumerator<dynamic?>.

Return()

Inserts a return; statement into the target code.

Return(dynamic?)

Inserts a return statement into the target code. This can be used to return a value from void-returning template methods.

RunTime<T>(T?)

Converts a compile-value into run-time value by serializing the compile-time value into a some syntax that will evaluate, at run time, to the same value as at compile time.

See Also