使用Delphi RTTI获取接口的字符串名称
我已经证明我可以使用 Delphi 2010 从其 GUID 获取接口的名称(例如 IMyInterface 转换为字符串“IMyInterface”。我想在 Delphi 7 中实现此目的(为了兼容性)。这可能吗?或者是存在基本的编译器限制。
I have proved that I can get the name of an interface from its GUID using Delphi 2010 (eg IMyInterface converted to the string 'IMyInterface'. I'd like to achieve this in Delphi 7 (for compatibility). Is this possible? Or are there fundamental compiler limitations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以获取它,下面显示了使用 IExample 类型如何获取名称。
旧的 Delphi 7 RTTI 是通过 TypInfo Unit 完成的。
刚刚注意到您说您想从 GUID 中获取它,而不仅仅是类型。这需要类型的 GUID 注册表。 Delphi 7 中的 RTTI 可用于获取类型。
下面将以IExample返回guid。
下面是一个示例注册表,它将接口的 TypeInfo() 映射到它的 GUID。
它可以优化,但我这样做是为了说明这个概念。
要将项目添加到注册表,您可以调用
Yes you can get it, the following shows using the IExample type how you can get the name.
The old Delphi 7 RTTI was done through the TypInfo Unit.
Just noticed you said you wanted to get it from the GUID and not just the type. This would require a registry of GUID to types. The RTTI in Delphi 7 can be used to get the type.
The following will take IExample return the guid.
Here is an example registry that would Map TypeInfo() of an Interface to it's GUID.
It could be optimized but I did it to illustrate the concept.
To Add an item to the registry you would then call
在 Delphi 7 中,您应该构建自己的从 GUID 到 RTTI(或名称)的映射。这里没有像 Delphi 2010 那样的 RTTI 上下文。我广泛使用 RIIT,通常在单元的初始化部分的某个中心位置“注册”所有有趣的类型,并根据 typeinfo 指针从那里查找所有类型。这适用于 D7、D2007 和 D2010(但如果您需要创建它,则需要更多工作)。此外,您可能会忘记注册类型并想知道为什么找不到某些东西。
In Delphi 7 you should build your own mapping from GUID to RTTI (or Name). THere is no RTTI context like in Delphi 2010. I use RIIT extensively and usually "register" all interesting types in the initialization section of the unit somewhere central and find all types from there based upon the typeinfo pointer. This works for D7, D2007 and D2010 (but is more work if you need to create it). Also you might forget to register a type and wonder why o why something cannot be found.