Edit

Share via


CodeTypeReference Class

Definition

Represents a reference to a type.

public ref class CodeTypeReference : System::CodeDom::CodeObject
public class CodeTypeReference : System.CodeDom.CodeObject
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeTypeReference : System.CodeDom.CodeObject
type CodeTypeReference = class
    inherit CodeObject
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeTypeReference = class
    inherit CodeObject
Public Class CodeTypeReference
Inherits CodeObject
Inheritance
CodeTypeReference
Attributes

Examples

The following example demonstrates use of a CodeTypeReference to represent a reference to a type.

// Creates a reference to the System.DateTime type.
CodeTypeReference^ typeRef1 = gcnew CodeTypeReference("System.DateTime");

// Creates a typeof expression for the specified type reference.
CodeTypeOfExpression^ typeof1 = gcnew CodeTypeOfExpression(typeRef1);

// Create a C# code provider
CodeDomProvider^ provider = CodeDomProvider::CreateProvider("CSharp");

// Generate code and send the output to the console
provider->GenerateCodeFromExpression(typeof1, Console::Out, gcnew CodeGeneratorOptions());
// The code generator produces the following source code for the preceeding example code:
//    typeof(System.DateTime)
// Creates a reference to the System.DateTime type.
CodeTypeReference typeRef1 = new CodeTypeReference("System.DateTime");

// Creates a typeof expression for the specified type reference.
CodeTypeOfExpression typeof1 = new CodeTypeOfExpression(typeRef1);

// Create a C# code provider
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

// Generate code and send the output to the console
provider.GenerateCodeFromExpression(typeof1, Console.Out, new CodeGeneratorOptions());
// The code generator produces the following source code for the preceeding example code:
//    typeof(System.DateTime)
' Creates a reference to the System.DateTime type.
Dim typeRef1 As New CodeTypeReference("System.DateTime")

' Creates a typeof expression for the specified type reference.
Dim typeof1 As New CodeTypeOfExpression(typeRef1)

' Create a Visual Basic code provider
Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")

' Generate code and send the output to the console
provider.GenerateCodeFromExpression(typeof1, Console.Out, new CodeGeneratorOptions())
' The code generator produces the following source code for the preceeding example code:
'    GetType(Date)

Remarks

A CodeTypeReference object is used to represent a type for CodeDOM objects. When CodeDOM types have a Type property, it is of type CodeTypeReference. For example, the CodeMemberField.Type property is a CodeTypeReference that represents a field's data type.

A CodeTypeReference can be initialized with a Type object or a string. It is generally recommended to use a Type to do this, although it may not always be possible. If initializing an instance of this class with a string, it is strongly recommended to always use fully qualified types, such as "System.Console" instead of just "Console", because not all languages support importing namespaces. Array types can be specified by either passing in a type object for an array or using one of the constructors that accept rank as a parameter.

The BaseType property specifies the name of the type to reference. For references to array types, the ArrayElementType property indicates the type of the elements of the array, and the ArrayRank property indicates the number of dimensions in the array.

Constructors

CodeTypeReference()

Initializes a new instance of the CodeTypeReference class.

CodeTypeReference(CodeTypeParameter)

Initializes a new instance of the CodeTypeReference class using the specified code type parameter.

CodeTypeReference(CodeTypeReference, Int32)

Initializes a new instance of the CodeTypeReference class using the specified array type and rank.

CodeTypeReference(String)

Initializes a new instance of the CodeTypeReference class using the specified type name.

CodeTypeReference(String, CodeTypeReference[])

Initializes a new instance of the CodeTypeReference class using the specified type name and type arguments.

CodeTypeReference(String, CodeTypeReferenceOptions)

Initializes a new instance of the CodeTypeReference class using the specified type name and code type reference option.

CodeTypeReference(String, Int32)

Initializes a new instance of the CodeTypeReference class using the specified array type name and rank.

CodeTypeReference(Type)

Initializes a new instance of the CodeTypeReference class using the specified type.

CodeTypeReference(Type, CodeTypeReferenceOptions)

Initializes a new instance of the CodeTypeReference class using the specified type and code type reference.

Properties

ArrayElementType

Gets or sets the type of the elements in the array.

ArrayRank

Gets or sets the array rank of the array.

BaseType

Gets or sets the name of the type being referenced.

Options

Gets or sets the code type reference option.

TypeArguments

Gets the type arguments for the current generic type reference.

UserData

Gets the user-definable data for the current object.

(Inherited from CodeObject)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to