TypeReference Class

Represents a reference to a type, including support for generics, arrays, and nullable annotations.

public class TypeReference

Example

var stringType = new TypeReference(typeof(string));
var listOfInt = new TypeReference(typeof(List<>)).MakeGeneric(new TypeReference(typeof(int)));
var intArray = new TypeReference(typeof(int)).MakeArray(1);

Assert.Equal("System.String", stringType.ClrFullTypeName);
Assert.Equal("System.Collections.Generic.List<System.Int32>", listOfInt.ClrFullTypeName);
Assert.Equal("System.Int32[]", intArray.ClrFullTypeName);

Constructors

NameDescription
TypeReference(TypeDeclaration)Initializes a new instance of the TypeReference class from a type declaration.
TypeReference(TypeParameter)Initializes a new instance of the TypeReference class from a type parameter.
TypeReference(string)Initializes a new instance of the TypeReference class from a type name.
TypeReference(Type)Initializes a new instance of the TypeReference class from a reflection type.

TypeReference overloads

TypeReference(TypeDeclaration)

TypeReference.cs

Initializes a new instance of the TypeReference class from a type declaration.

public TypeReference(TypeDeclaration typeDeclaration)
Parameters
typeDeclaration TypeDeclaration
The type declaration to reference.

TypeReference(TypeParameter)

TypeReference.cs

Initializes a new instance of the TypeReference class from a type parameter.

public TypeReference(TypeParameter typeParameter)
Parameters
typeParameter TypeParameter
The type parameter to reference.

TypeReference(string)

TypeReference.cs

Initializes a new instance of the TypeReference class from a type name.

public TypeReference(string typeName)
Parameters
typeName string
The fully qualified type name.

TypeReference(Type)

TypeReference.cs

Initializes a new instance of the TypeReference class from a reflection type.

public TypeReference(Type type)
Parameters
type Type
The reflection type to reference.

Properties

NameDescription
ArrayRankGets or sets the array rank (0 for non-arrays, 1 for single-dimension arrays, etc.).
ClrFullTypeNameGets the full CLR type name including generic parameters and array notation.
IsArrayGets a value indicating whether this type reference represents an array.
NullableGets or sets the nullable annotation for this type reference.
ParametersGets the list of generic type parameters for this type reference.
TypeNameGets the fully qualified name of the referenced type.

Gets or sets the array rank (0 for non-arrays, 1 for single-dimension arrays, etc.).

public int ArrayRank { get; set; }

Property value

int

ClrFullTypeName

TypeReference.cs

Gets the full CLR type name including generic parameters and array notation.

public string ClrFullTypeName { get; }

Property value

string

Gets a value indicating whether this type reference represents an array.

public bool IsArray { get; }

Property value

bool

Gets or sets the nullable annotation for this type reference.

public NullableAnnotation Nullable { get; set; }

Property value

NullableAnnotation

Gets the list of generic type parameters for this type reference.

public IList<TypeReference> Parameters { get; }

Property value

IList<TypeReference>

Gets the fully qualified name of the referenced type.

public string? TypeName { get; }

Property value

string?

Methods

NameDescription
Clone()Creates a shallow copy of this type reference.
MakeArray(int)Creates an array type reference with the specified rank.
MakeGeneric(TypeReference[])Creates a generic type reference with the specified type arguments.
MakeNullable(NullableAnnotation)Creates a nullable type reference with the specified nullable annotation.

Creates a shallow copy of this type reference.

public TypeReference Clone()

Returns

TypeReference A new TypeReference instance with the same values.

MakeArray(int)

TypeReference.cs

Creates an array type reference with the specified rank.

public TypeReference MakeArray(int rank)

Parameters

rank int
The array rank (1 for single-dimension, 2 for two-dimensional, etc.).

Returns

TypeReference A new array TypeReference instance.

MakeGeneric(TypeReference[])

TypeReference.cs

Creates a generic type reference with the specified type arguments.

public TypeReference MakeGeneric(params TypeReference[] typeArguments)

Parameters

typeArguments TypeReference[]
The type arguments for the generic type.

Returns

TypeReference A new generic TypeReference instance.

MakeNullable(NullableAnnotation)

TypeReference.cs

Creates a nullable type reference with the specified nullable annotation.

public TypeReference MakeNullable(NullableAnnotation value = NullableAnnotation.Nullable)

Parameters

value NullableAnnotation
The nullable annotation to apply.

Returns

TypeReference A new nullable TypeReference instance.

Operators

NameDescription
implicit operator TypeReference(TypeDeclaration)
implicit operator TypeReference(TypeParameter)
implicit operator TypeReference(Type)

op_Implicit overloads

implicit operator TypeReference(TypeDeclaration)

TypeReference.cs
public static implicit operator TypeReference(TypeDeclaration typeDeclaration)
Parameters
typeDeclaration TypeDeclaration
Returns

TypeReference

No documentation is available.

implicit operator TypeReference(TypeParameter)

TypeReference.cs
public static implicit operator TypeReference(TypeParameter type)
Parameters
type TypeParameter
Returns

TypeReference

No documentation is available.

implicit operator TypeReference(Type)

TypeReference.cs
public static implicit operator TypeReference(Type type)
Parameters
type Type
Returns

TypeReference

No documentation is available.