TypeReference Class
Represents a reference to a type, including support for generics, arrays, and nullable annotations.
public class TypeReferenceExample
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
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the TypeReference class from a type declaration.
public TypeReference(TypeDeclaration typeDeclaration)Parameters
typeDeclarationTypeDeclaration- The type declaration to reference.
TypeReference(TypeParameter)
TypeReference.csInitializes a new instance of the TypeReference class from a type parameter.
public TypeReference(TypeParameter typeParameter)Parameters
typeParameterTypeParameter- The type parameter to reference.
TypeReference(string)
TypeReference.csInitializes a new instance of the TypeReference class from a type name.
public TypeReference(string typeName)Parameters
typeNamestring- The fully qualified type name.
TypeReference(Type)
TypeReference.csInitializes a new instance of the TypeReference class from a reflection type.
public TypeReference(Type type)Parameters
typeType- The reflection type to reference.
Properties
| Name | Description |
|---|---|
| ArrayRank | Gets or sets the array rank (0 for non-arrays, 1 for single-dimension arrays, etc.). |
| ClrFullTypeName | Gets the full CLR type name including generic parameters and array notation. |
| IsArray | Gets a value indicating whether this type reference represents an array. |
| Nullable | Gets or sets the nullable annotation for this type reference. |
| Parameters | Gets the list of generic type parameters for this type reference. |
| TypeName | Gets the fully qualified name of the referenced type. |
ArrayRank
TypeReference.csClrFullTypeName
TypeReference.csIsArray
TypeReference.csNullable
TypeReference.csGets or sets the nullable annotation for this type reference.
public NullableAnnotation Nullable { get; set; }Property value
Parameters
TypeReference.csGets the list of generic type parameters for this type reference.
public IList<TypeReference> Parameters { get; }Property value
TypeName
TypeReference.csMethods
| Name | Description |
|---|---|
| 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. |
Clone()
TypeReference.csCreates a shallow copy of this type reference.
public TypeReference Clone()Returns
TypeReference A new TypeReference instance with the same values.
MakeArray(int)
TypeReference.csCreates an array type reference with the specified rank.
public TypeReference MakeArray(int rank)Parameters
rankint- The array rank (1 for single-dimension, 2 for two-dimensional, etc.).
Returns
TypeReference A new array TypeReference instance.
MakeGeneric(TypeReference[])
TypeReference.csCreates a generic type reference with the specified type arguments.
public TypeReference MakeGeneric(params TypeReference[] typeArguments)Parameters
typeArgumentsTypeReference[]- The type arguments for the generic type.
Returns
TypeReference A new generic TypeReference instance.
MakeNullable(NullableAnnotation)
TypeReference.csCreates a nullable type reference with the specified nullable annotation.
public TypeReference MakeNullable(NullableAnnotation value = NullableAnnotation.Nullable)Parameters
valueNullableAnnotation- The nullable annotation to apply.
Returns
TypeReference A new nullable TypeReference instance.
Operators
| Name | Description |
|---|---|
| implicit operator TypeReference(TypeDeclaration) | |
| implicit operator TypeReference(TypeParameter) | |
| implicit operator TypeReference(Type) |
op_Implicit overloads
implicit operator TypeReference(TypeDeclaration)
TypeReference.cspublic static implicit operator TypeReference(TypeDeclaration typeDeclaration)No documentation is available.
implicit operator TypeReference(TypeParameter)
TypeReference.cspublic static implicit operator TypeReference(TypeParameter type)No documentation is available.
implicit operator TypeReference(Type)
TypeReference.cspublic static implicit operator TypeReference(Type type)No documentation is available.