CodeTypeReference 类

定义

表示对某类型的引用。Represents a reference to a type.

public ref class CodeTypeReference : System::CodeDom::CodeObject
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeTypeReference : System.CodeDom.CodeObject
type CodeTypeReference = class
    inherit CodeObject
Public Class CodeTypeReference
Inherits CodeObject
继承
CodeTypeReference
属性

示例

下面的示例演示如何使用CodeTypeReference来表示对类型的引用。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)

注解

CodeTypeReference对象用于表示 CodeDOM 对象的类型。A CodeTypeReference object is used to represent a type for CodeDOM objects. 当 CodeDOM 类型具有Type属性时, 它的类型CodeTypeReference为。When CodeDOM types have a Type property, it is of type CodeTypeReference. 例如, CodeMemberField.Type属性是一个CodeTypeReference表示字段的数据类型的。For example, the CodeMemberField.Type property is a CodeTypeReference that represents a field's data type.

CodeTypeReference 可以Type使用对象或字符串来初始化。A CodeTypeReference can be initialized with a Type object or a string. 通常建议使用Type来执行此操作, 尽管并非始终都可以这样做。It is generally recommended to use a Type to do this, although it may not always be possible. 如果使用字符串初始化此类的实例, 则强烈建议始终使用完全限定的类型 (如 "System.web") 而不是仅使用 "控制台", 因为并非所有语言都支持导入命名空间。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. 可以通过传入数组的类型对象或使用接受 rank 作为参数的构造函数之一来指定数组类型。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.

BaseType属性指定要引用的类型的名称。The BaseType property specifies the name of the type to reference. 对于对数组类型的引用, ArrayElementType属性指示数组元素的类型, ArrayRank属性指示数组中的维数。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.

构造函数

CodeTypeReference()

初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class.

CodeTypeReference(CodeTypeParameter)

使用指定的代码类型参数初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified code type parameter.

CodeTypeReference(CodeTypeReference, Int32)

使用指定的数组类型和秩初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified array type and rank.

CodeTypeReference(String)

使用指定的类型名称初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified type name.

CodeTypeReference(String, CodeTypeReference[])

使用指定的类型名称和类型参数初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified type name and type arguments.

CodeTypeReference(String, CodeTypeReferenceOptions)

使用指定的类型名称和代码类型引用选项初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified type name and code type reference option.

CodeTypeReference(String, Int32)

使用指定的数组类型名称和秩初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified array type name and rank.

CodeTypeReference(Type)

使用指定的类型初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified type.

CodeTypeReference(Type, CodeTypeReferenceOptions)

使用指定的类型和代码类型引用初始化 CodeTypeReference 类的新实例。Initializes a new instance of the CodeTypeReference class using the specified type and code type reference.

属性

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.

(继承自 CodeObject)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于