Open sandboxFocus

Struct TypedConstant

Represents a typed value that can be defined, defined to null, or undefined. Used to represent default values, for instance DefaultValue, or attribute arguments.

Namespace: Metalama.Framework.Code
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public readonly struct TypedConstant : IExpression, IHasType, IEquatable<TypedConstant>

Properties

Name Description
HasNullForgivingOperator

Gets a value indicating whether the null-forgiving operator (!) should be appended when this TypedConstant is rendered to code. Only applicable when IsNullOrDefault is true.

IsArray
IsInitialized

Gets a value indicating whether the Value has been specified (including when it is set to null).

IsNullOrDefault

Gets a value indicating whether the value is null or default. Not to be confused with IsInitialized.

RawValue

Gets the raw value of the TypedConstant. If IsArray is true, this property returns an ImmutableArray<TypedConstant>.

Type

Gets the type of the value. This is important if the type is an enum, because in this case, if the enum type is not compile-time, Value is set to the underlying integer value.

Value

Gets the Value for non-array types. For array types, get an array of a primitive type (e.g. int[]) instead of an array of TypedConstant.

Values

Methods

Name Description
Create(IField)

Creates a TypedConstant that references a constant field. When rendered to code, this produces a reference to the field (e.g., MyClass.MyConstField) instead of the literal value.

Create(object?)
Create(object?, IType?)
Create(object?, Type?)
CreateUnchecked(object?, IType)
Default(IType, bool)

Creates a TypedConstant representing the default value for the specified type. When rendered to code, this produces default(T) or default(T)! if hasNullForgivingOperator is true.

Default(Type, bool)

Creates a TypedConstant representing the default value for the specified type. When rendered to code, this produces default(T) or default(T)! if hasNullForgivingOperator is true.

Equals(TypedConstant)
Equals(object?)
ForCompilation(ICompilation)
GetHashCode()
NamedConstant(IField)

Creates a TypedConstant that references a constant field. This is equivalent to Create(IField). When rendered to code, this produces a reference to the field (e.g., MyEnum.MyValue or MyClass.MyConst).

NamedConstant(INamedType, string)

Creates a TypedConstant that references a named constant (such as an enum member or a const field) by name. When rendered to code, this produces a reference to the field (e.g., MyEnum.MyValue or MyClass.MyConst).

NamedConstant(Type, string)

Creates a TypedConstant that references a named constant (such as an enum member or a const field) by name. When rendered to code, this produces a reference to the field (e.g., MyEnum.MyValue or MyClass.MyConst).

ToString()
TryConvertFromExpression(IExpression, out TypedConstant?)
TryConvertFromExpression(string, out TypedConstant?)

Operators

Name Description
operator ==(TypedConstant, TypedConstant)
explicit operator TypedConstant(string?)

Converts a string value into a TypedConstant of type string. A null value gives a null constant of type string.

implicit operator TypedConstant(bool)

Converts a bool value into a TypedConstant of type bool.

implicit operator TypedConstant(byte)

Converts a byte value into a TypedConstant of type byte.

implicit operator TypedConstant(char)

Converts a char value into a TypedConstant of type char.

implicit operator TypedConstant(decimal)

Converts a decimal value into a TypedConstant of type decimal.

implicit operator TypedConstant(double)

Converts a double value into a TypedConstant of type double.

implicit operator TypedConstant(short)

Converts a short value into a TypedConstant of type short.

implicit operator TypedConstant(int)

Converts a int value into a TypedConstant of type int.

implicit operator TypedConstant(long)

Converts a long value into a TypedConstant of type long.

implicit operator TypedConstant(sbyte)

Converts a sbyte value into a TypedConstant of type sbyte.

implicit operator TypedConstant(float)

Converts a float value into a TypedConstant of type float.

implicit operator TypedConstant(ushort)

Converts a ushort value into a TypedConstant of type ushort.

implicit operator TypedConstant(uint)

Converts a uint value into a TypedConstant of type uint.

implicit operator TypedConstant(ulong)

Converts a ulong value into a TypedConstant of type ulong.

operator !=(TypedConstant, TypedConstant)

Extension Methods

See Also