Open sandboxFocus

Method NamedConstant

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).

Declaration
public static TypedConstant NamedConstant(Type declaringType, string memberName)
Parameters
Type Name Description
Type declaringType

The reflection Type of the type declaring the constant. Must be a named type.

string memberName

The name of the constant field or enum member.

Returns
Type Description
TypedConstant

A TypedConstant representing the named constant reference.

Exceptions
Type Condition
ArgumentException

Thrown when memberName does not correspond to a constant member of the type.

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).

Declaration
public static TypedConstant NamedConstant(INamedType declaringType, string memberName)
Parameters
Type Name Description
INamedType declaringType

The INamedType of the type declaring the constant.

string memberName

The name of the constant field or enum member.

Returns
Type Description
TypedConstant

A TypedConstant representing the named constant reference.

Exceptions
Type Condition
ArgumentException

Thrown when memberName does not correspond to a constant member of the type.

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).

Declaration
public static TypedConstant NamedConstant(IField field)
Parameters
Type Name Description
IField field

A constant field, i.e., ConstantValue must not be null.

Returns
Type Description
TypedConstant

A TypedConstant representing the named constant reference.

Exceptions
Type Condition
ArgumentException

Thrown when field is not a constant field.