Open sandboxFocus

Method DefineLocalVariable

DefineLocalVariable(string, IType)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, IType type)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

IType type

The type of the variable.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

Remarks

Use this method when you need to programmatically define local variables, for example, within a compile-time foreach loop where the number of variables is determined at compile-time. Unlike normal template variable declarations which are automatically classified as run-time or compile-time, DefineLocalVariable(string, IType) always creates a run-time variable.

The returned IExpression can be used to read or write the variable value in subsequent template code.

See Also

DefineLocalVariable(string, IType, IExpression?)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, IType type, IExpression? value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

IType type

The type of the variable.

IExpression value

An expression representing the initial value, or null for no initialization.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

Remarks

Use this method when you need to programmatically define local variables with compile-time determined initial values. This is useful when generating code that saves and restores values, such as rolling back field changes upon exception.

See Also

DefineLocalVariable(string, IType, dynamic)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, IType type, dynamic value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

IType type

The type of the variable.

dynamic value

The initial value for the variable.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

See Also

DefineLocalVariable(string, Type)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, Type type)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

Type type

The reflection type of the variable.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

See Also

DefineLocalVariable(string, Type, IExpression?)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, Type type, IExpression? value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

Type type

The reflection type of the variable.

IExpression value

An expression representing the initial value, or null for no initialization.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

See Also

DefineLocalVariable(string, Type, dynamic)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, Type type, dynamic value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

Type type

The reflection type of the variable.

dynamic value

The initial value for the variable.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

See Also

DefineLocalVariable(string, IExpression)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, IExpression value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

IExpression value

An expression representing the initial value. The type is inferred from this expression.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

Remarks

This overload infers the variable type from the value expression, similar to using var in C#.

See Also

DefineLocalVariable(string, dynamic)

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

Declaration
[CompileTime(true, null)]
public static IExpression DefineLocalVariable(string nameHint, dynamic value)
Parameters
Type Name Description
string nameHint

A hint for the variable name. Metalama automatically appends a numerical suffix to ensure uniqueness within the target lexical scope.

dynamic value

The initial value. The type is inferred from this value.

Returns
Type Description
IExpression

An IExpression that can be used to reference the local variable in run-time code.

Remarks

This overload infers the variable type from the value, similar to using var in C#.

See Also