适用于 .NET 和 UWP 的组件扩展Component Extensions for .NET and UWP

C++ 标准允许编译器供应商向语言提供非标准扩展。The C++ standard allows compiler vendors to provide non-standard extensions to the language. Microsoft 提供了有助于将本机 C++ 代码连接到 .NET Framework 或通用 Windows 平台 (UWP) 上运行的代码的扩展。Microsoft provides extensions to help you connect native C++ code to code that runs on the .NET Framework or the Universal Windows Platform (UWP). .NET 扩展称为“C++/CLI”,并生成在 .NET 托管执行环境(称为“公共语言运行时 (CLR)”)中执行的代码。The .NET extensions are called C++/CLI and produce code that executes in the .NET managed execution environment that is called the Common Language Runtime (CLR). UWP 扩展称为“C++/CX”,并生成本机代码。The UWP extensions are called C++/CX and they produce native machine code.

备注

对于新应用程序,建议使用 C++/WinRT,而不是 C++/CX。For new applications, we recommend using C++/WinRT rather than C++/CX. C++/ WinRT 是 Windows 运行时 API 的新标准 C++17 语言投影。C++/WinRT is a new, standard C++17 language projection for Windows Runtime APIs. 我们将继续支持 C++/CX 和 WRL,但强烈建议新应用程序使用 C++/WinRT。We will continue to support C++/CX and WRL, but highly recommend that new applications use C++/WinRT. 有关详细信息,请参阅 C++/WinRTFor more information, see C++/WinRT.

两个运行时,一组扩展Two runtimes, one set of extensions

C++/CLI 扩展了 ISO/ANSI C++ 标准,并在 ECMA C++/CLI 标准下定义。C++/CLI extends the ISO/ANSI C++ standard, and is defined under the Ecma C++/CLI Standard. 有关详细信息,请参阅使用 C++/CLI 进行 .NET 编程 (Visual C++)For more information, see .NET Programming with C++/CLI (Visual C++).

C++/CX 扩展是 C++/CLI 的子集。The C++/CX extensions are a subset of C++/CLI. 虽然扩展语法在大多数情况下是完全相同的,但具体生成什么代码还是取决于你是将 /ZW 编译器选项指定给目标 UWP,还是将 /clr 选项指定给目标 .NET。Although the extension syntax is identical in most cases, the code that is generated depends on whether you specify the /ZW compiler option to target UWP, or the /clr option to target .NET. 当使用 Visual Studio 创建项目时,将自动设置这些开关。These switches are set automatically when you use Visual Studio to create a project.

数据类型关键字Data Type Keywords

语言扩展包括聚合关键字,它是由用空格分隔的两个标记组成。The language extensions include aggregate keywords, which consist of two tokens separated by white space. 标记单独使用时可能表示一种含义,一起使用时可能表示另一种含义。The tokens might have one meaning when they are used separately, and another meaning when they are used together. 例如,单词“ref”是一个普通的标识符,单词“class”是一个声明本机类的关键字。For example, the word "ref" is an ordinary identifier, and the word "class" is a keyword that declares a native class. 不过,如果将这两个字词组合成 ref class,生成的聚合关键字声明称为“运行时类”的实体。But when these words are combined to form ref class, the resulting aggregate keyword declares an entity that is known as a runtime class.

扩展还包括上下文相关关键字。The extensions also include context-sensitive keywords. 某个关键字被视为区分上下文,即表示其语义取决于包含它的语句类型及其在该语句中的位置。A keyword is treated as context-sensitive depending on the kind of statement that contains it, and its placement in that statement. 例如,标记“property”既可以是标识符,也可以声明特殊类型的公共类成员。For example, the token "property" can be an identifier, or it can declare a special kind of public class member.

下表列出了 C++ 语言扩展中的关键字。The following table lists keywords in the C++ language extension.

关键字Keyword 区分上下文Context sensitive 目标Purpose 参考Reference
ref classref class

ref structref struct
NoNo 声明类。Declares a class. 类和结构Classes and Structs
value classvalue class

value structvalue struct
NoNo 声明值类。Declares a value class. 类和结构Classes and Structs
接口类interface class

interface structinterface struct
NoNo 声明接口。Declares an interface. 接口类interface class
枚举类enum class

enum structenum struct
NoNo 声明枚举。Declares an enumeration. 枚举类enum class
属性property Yes 声明属性。Declares a property. 属性property
delegatedelegate Yes 声明委托。Declares a delegate. 委托 (C++/CLI 和 C++/CX)delegate (C++/CLI and C++/CX)
eventevent Yes 声明事件。Declares an event. eventevent

重写说明符Override Specifiers

可以使用下列关键字来限定派生的替代行为。You can use the following keywords to qualify override behavior for derivation. 虽然 new 关键字不是 C++ 的扩展,但仍将它列于此处是因为它可用于其他上下文。Although the new keyword is not an extension of C++, it is listed here because it can be used in an additional context. 某些说明符还可用于本机编程。Some specifiers are also valid for native programming. 有关详细信息,请参阅如何:在本机编译中声明重写说明符 (C++/CLI)For more information, see How to: Declare Override Specifiers in Native Compilations (C++/CLI).

关键字Keyword 区分上下文Context Sensitive 目标Purpose 参考Reference
abstractabstract Yes 指示函数或类是抽象的。Indicates that functions or classes are abstract. abstractabstract
newnew NoNo 指示函数不替代基类版本。Indicates that a function is not an override of a base class version. 新(vtable 中的新槽)new (new slot in vtable)
overrideoverride Yes 指示方法必须替代基类版本。Indicates that a method must be an override of a base-class version. overrideoverride
sealedsealed Yes 防止类用作基类。Prevents classes from being used as base classes. sealedsealed

泛型关键字Keywords for Generics

已添加下列关键字来支持泛型类型。The following keywords have been added to support generic types. 有关详细信息,请参阅泛型For more information, see Generics.

关键字Keyword 区分上下文Context sensitive 目标Purpose
genericgeneric NoNo 声明泛型类型。Declares a generic type.
wherewhere Yes 指定应用于泛型类型参数的约束。Specifies the constraints that are applied to a generic type parameter.

杂项关键字Miscellaneous Keywords

C++ 扩展中已添加下列关键字。The following keywords have been added to the C++ extensions.

关键字Keyword 区分上下文Context sensitive 目标Purpose 参考Reference
finallyfinally Yes 指示默认异常处理行为。Indicates default exception handlings behavior. 异常处理Exception Handling
for each, infor each, in NoNo 枚举集合元素。Enumerates elements of a collection. for each, infor each, in
gcnewgcnew NoNo 分配垃圾回收堆上的类型。Allocates types on the garbage-collected heap. 用它代替 new 和 delete。Use instead of new and delete. ref new、gcnewref new, gcnew
ref newref new Yes 分配 Windows 运行时类型。Allocates a Windows Runtime type. 用它代替 new 和 delete。Use instead of new and delete. ref new、gcnewref new, gcnew
initonlyinitonly Yes 指示只能在声明时或在静态构造函数中初始化成员。Indicates that a member can only be initialized at declaration or in a static constructor. initonly (C++/CLI)initonly (C++/CLI)
名称literal Yes 创建文本变量。Creates a literal variable. 名称literal
nullptrnullptr NoNo 指示图柄或指针不指向对象。Indicates that a handle or pointer does not point at an object. nullptrnullptr

模板构造Template Constructs

下列语言构造作为模板而非关键字实现。The following language constructs are implemented as templates, instead of as keywords. 如果指定 /ZW 编译器选项,它们将在 lang 命名空间中定义。If you specify the /ZW compiler option, they are defined in the lang namespace. 如果指定 /clr 编译器选项,它们将在 cli 命名空间中定义。If you specify the /clr compiler option, they are defined in the cli namespace.

关键字Keyword 目标Purpose 参考Reference
arrayarray 声明数组。Declares an array. 数组Arrays
interior_ptrinterior_ptr (仅限 CLR)指向引用类型中的数据。(CLR only) Points to data in a reference type. interior_ptr (C++/CLI)interior_ptr (C++/CLI)
pin_ptrpin_ptr (仅限 CLR)指向 CLR 引用类型以便暂时抑制垃圾回收系统。(CLR only) Points to CLR reference types to temporarily suppress the garbage-collection system. pin_ptr (C++/CLI)pin_ptr (C++/CLI)
safe_castsafe_cast 确定并执行运行时类型的最佳转换方法。Determines and executes the optimal casting method for a runtime type. safe_castsafe_cast
typeidtypeid (仅限 CLR)检索用于描述给定类型或对象的 System.Type 对象。(CLR only) Retrieves a System.Type object that describes the given type or object. typeidtypeid

声明符Declarators

下列类型声明符指示运行时自动管理已分配对象的生存期和删除。The following type declarators instruct the runtime to automatically manage the lifetime and deletion of allocated objects.

运算符Operator 目标Purpose 参考Reference
^ 声明指向对象的句柄;即指向 Windows 运行时或不再可用时自动删除的 CLR 对象的指针。Declares a handle to an object; that is, a pointer to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. 对象句柄运算符 (^)Handle to Object Operator (^)
% 声明跟踪引用;即对 Windows 运行时或不再可用时自动删除的 CLR 对象的引用。Declares a tracking reference; that is, a reference to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. 跟踪引用运算符Tracking Reference Operator

本节列出了其他编程构造以及与 CLR 相关的主题。This section lists additional programming constructs, and topics that pertain to the CLR.

主题Topic 说明Description
__identifier (C++/CLI)__identifier (C++/CLI) (Windows 运行时和 CLR)允许使用关键字作为标识符。(Windows Runtime and CLR) Enables the use of keywords as identifiers.
变量自变量列表 (...) (C++/CLI)Variable Argument Lists (...) (C++/CLI) (Windows 运行时和 CLR)允许函数使用可变数量参数。(Windows Runtime and CLR) Enables a function to take a variable number of arguments.
对应于 C++ 本机类型的 .NET Framework 类型 (C++/CLI).NET Framework Equivalents to C++ Native Types (C++/CLI) 列出替代 C++ 整型类型的 CLR 类型。Lists the CLR types that are used in place of C++ integral types.
appdomain __declspec 修饰符appdomain __declspec modifier 规定每个 AppDomain 都必须存在静态变量和全局变量的 __declspec 修饰符。__declspec modifier that mandates that static and global variables exist per appdomain.
使用 /clr 时的 C 样式强制转换 (C++/CLI)C-Style Casts with /clr (C++/CLI) 描述如何解释 C 样式转换。Describes how C-style casts are interpreted.
__clrcall 调用约定__clrcall calling convention 指示符合 CLS 的调用约定。Indicates the CLR-compliant calling convention.
__cplusplus_cli 预定义宏Predefined Macros
自定义属性Custom Attributes 描述如何定义自己的 CLR 属性。Describes how to define your own CLR attributes.
异常处理Exception Handling 概述异常处理。Provides an overview of exception handling.
显式重写Explicit Overrides 演示成员函数如何替代任意成员。Demonstrates how member functions can override arbitrary members.
友元程序集 (C++)Friend Assemblies (C++) 讨论客户端程序集如何访问程序集组件中的所有类型。Discusses how a client assembly can access all types in an assembly component.
装箱Boxing 演示值类型进行装箱的条件。Demonstrates the conditions in which values types are boxed.
编译器对类型特征的支持Compiler Support for Type Traits 讨论如何在编译时检测类型的特征。Discusses how to detect characteristics of types at compile time.
managed、unmanaged 杂注managed, unmanaged pragmas 演示 managed 和 unmanaged 函数如何共存于同一模块中。Demonstrates how managed and unmanaged functions can co-exist in the same module.
process __declspec 修饰符process __declspec modifier 规定每个 process 都必须存在静态变量和全局变量的 __declspec 修饰符。__declspec modifier that mandates that static and global variables exist per process.
反射 (C++/CLI)Reflection (C++/CLI) 演示运行时类型信息的 CLR 版本。Demonstrates the CLR version of run-time type information.
字符串String 讨论编译器如何将字符串文本转换为 StringDiscusses compiler conversion of string literals to String.
类型转发 (C++/CLI)Type Forwarding (C++/CLI) 允许将一个传送程序集中的类型移动到另一个程序集,从而使客户端代码无需重新编译。Enables the movement of a type in a shipping assembly to another assembly so that client code does not have to be recompiled.
用户定义的属性User-Defined Attributes 演示用户定义的属性。Demonstrates user-defined attributes.
#using 指令#using Directive 导入外部程序集。Imports external assemblies.
XML 文档XML Documentation 使用 /doc(处理文档注释)(C/C++) 解释基于 XML 的代码文档Explains XML-based code documentation by using /doc (Process Documentation Comments) (C/C++)

请参阅See also

使用 C++/CLI (Visual C++) 进行 .NET 编程.NET Programming with C++/CLI (Visual C++)
本机和 .NET 的互操作性Native and .NET Interoperability