CLR 集成 (C++/CX)

某些 Windows 运行时类型在 C++/CX 以及基于公共语言运行时 (CLR) 的语言中接收特殊处理。 本文讨论一种语言中的几种类型如何映射到另一种语言。 例如,CLR 将 Windows.Foundation.IVector 映射到 System.Collections.IList,将 Windows.Foundation.IMap 映射到 System.Collections.IDictionary,等等。 同样,C++/CX 特别映射了 Platform::Delegate 和 Platform::String 等类型。

将 Windows 运行时映射到 C++/CX

当 C++/CX 读取 Windows 元数据 (.winmd) 文件时,编译器会自动将公共 Windows 运行时命名空间和类型映射到 C++/CX 命名空间和类型。 例如,数值 Windows 运行时类型 UInt32 自动映射到 default::uint32

C++/CX 将其他几个 Windows 运行时类型映射到“平台”命名空间。 例如,表示只读 Unicode 文本字符串的 Windows::Foundation HSTRING 句柄映射到 C++/CX Platform::String 类。 当 Windows 运行时操作返回错误 HRESULT 时,它会映射到 C++/CX Platform::Exception

C++/CX 还会映射 Windows 运行时命名空间中的某些类型来增强类型的功能。 对于这些类型,C++/CX 提供特定于 C++ 且在类型的标准 .winmd 文件中不可用的帮助器构造函数和方法。

下表显示支持新构造函数和帮助器方法的值结构。 如果以前编写了使用结构初始化列表的代码,请将它更改为使用新添加的构造函数。

Windows::Foundation

  • Point

  • Rect

  • 大小

Windows::UI

  • 颜色

Windows::UI::Xaml

  • CornerRadius

  • Duration

  • GridLength

  • Thickness

Windows::UI::Xaml::Interop

  • TypeName

Windows::UI::Xaml::Media

  • Matrix

Windows::UI::Xaml::Media::Animation

  • KeyTime

  • RepeatBehavior

Windows::UI::Xaml::Media::Media3D

  • Matrix3D

将 CLR 映射到 C++/CX

当 Microsoft C++ 或 C# 编译器读取 .winmd 文件时,它们会将元数据文件中的某些类型自动映射到相应的 C++/CX 或 CLR 类型。 例如,在 CLR 中,IVector<T> 接口会映射到 IList<T>。 但在 C++/CX 中,IVector<T> 接口不会映射到另一种类型。

Windows 运行时中的 IReference<T> 会映射到 .NET 中的 Nullable<T>。

另请参阅

与其他语言进行互操作