WinRT 反射 (C++/CX)

发布于 2024-12-06 12:09:28 字数 176 浏览 3 评论 0原文

如何内省 C++/CX 中的对象?我知道如何获取其类名(使用 IInspectable),但我无法弄清楚如何获取其属性列表,或者如果我只有方法名称(字符串),则如何调用方法。我在这里和 Google 中搜索了答案,但我发现与 WinRT 的 .NET 层相关(System.Reflection 命名空间似乎在 C++/CX 中不可用)。

how can I introspect an object in C++/CX? I known how to get its class name (using IInspectable) but I wasn't able to figure out how to get a list of its properties or how to invoke methods if I have just a name of the method (string). I searched for an answer here and at Google but what I found is related to the .NET layer of WinRT (the System.Reflection namespace doesn't seem to be available in C++/CX).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生一梦 2024-12-13 12:09:28

正如 svick 所暗示的,您获取类名(从 IInspectable::GetRuntimeClassName 检索),将其交给 RoGetMetaDataFile。这将返回 IMetaDataImport2。现在调用 IMetaDataImport2::FindTypeDefByName。这将返回一个 typedef 标记。现在调用 IMetaDataImport2::GetTypeDefProps ,它将为您提供有关该类型的属性。

从 typedef 属性中,您可以检索其他信息 - 如果是接口/结构(或枚举),则枚举方法/字段,查找运行时类的类型(如果是接口或类)等。

As hinted by svick, you take the class name (retrieved from IInspectable::GetRuntimeClassName), hand it to RoGetMetaDataFile. This returns an IMetaDataImport2. Now call IMetaDataImport2::FindTypeDefByName. This returns a typedef token. Now call IMetaDataImport2::GetTypeDefProps which will give you properties about the type.

From the typedef properties, you can retrieve other information - enumerate the methods/fields if it's an interface/struct (or enum), find the type of the runtime class (if it's an interface or a class), etc.

荒芜了季节 2024-12-13 12:09:28

C++ 不提供任何特定的 API 来反映 WinRT 类型,这些类型在 CX 兼容的元数据文件中完全定义,您可以使用 CLR 本机元数据 API 来读取它们的定义。有一个片段

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/211ef583-db11-4e55-926b-6d9ab53dbdb4/ccx-reflection

James McNellis 发布了完整的 C++ 库去年的 CX 反思

http://seaplusplus.com/2012/04/ 26/cxxreflect-native-reflection-for-the-windows-runtime/

C++ doesn't provide any specific APIs to reflect on WinRT types, these types are fully defined in CX compliant metadata files and you can use the CLR native metadata APIs to read their definition. There is a snippet at

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/211ef583-db11-4e55-926b-6d9ab53dbdb4/ccx-reflection

James McNellis released a full C++ library for CX reflection last year

http://seaplusplus.com/2012/04/26/cxxreflect-native-reflection-for-the-windows-runtime/

提笔书几行 2024-12-13 12:09:28

即使大多数正常的 .Net 反射也不包含在 WinRT 应用程序可用的 .Net 子集中。我在 WinRT 文档中没有找到任何与反射相关的类型。这意味着(除非我忽略了某些事情)可用的 API 根本没有公开反射。

虽然我不明白为什么它不应该可用。元数据就在那里,应该足够了。

查看 C++ 特定函数时,有函数 RoGetMetaDataFile()。看来应该可以用它来获取元数据。但它是本机 C++ 函数,而不是 C++/CX。这意味着它不容易使用(手动内存管理……),而且我怀疑商店中的应用程序是否会允许它。

Even most of the normal .Net reflection isn't included in the subset of .Net available to WinRT applications. And I didn't find any reflection-related types in the WinRT documentation. This means that (unless I overlooked something) reflection is simply not exposed by the available APIs.

Although I don't see why it shouldn't be available. The metadata is there, which should be enough.

When looking at the C++-specific functions, there is the function RoGetMetaDataFile(). It seems it should be possible to use it to get the metadata. But it's a native C++ function, not C++/CX. This means it's not easy to use (manual memory management, …) and I doubt it will be allowed in apps that are in the Store.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文