枚举实现给定接口的所有 Delphi 类?
通过 Delphi 2010 中新扩展的 RTTI,Delphi 应用程序(在运行时)能否构建实现给定接口的所有类的列表?
With the new extended RTTI in Delphi 2010, can a Delphi application (at run time) build a list of all classes which implement a given interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲是可以的,只要接口有 GUID 并且您关心的类位于单元的接口部分中。
RTTI 单元(尚)没有用于描述类实现的所有接口的 API,但数据位于类型信息中。
接口类型的 GUID 可以从此表达式获取:
或者:
然后可以使用 TClass.GetInterfaceEntry(IID) 检查实例类型的类 - 如果它返回非零,则该类实现界面。
可以通过 TRttiContext.GetTypes 检索在单元的接口部分中声明的所有类。
编写实际的迭代和提取代码作为读者的练习。
Technically yes, so long as the interface has a GUID and the classes you care about are in the interface sections of units.
The RTTI unit does not (yet) have an API for describing all interfaces that a class implements, but the data is in the type info.
The GUID for an interface type can be gotten from this expression:
or:
The class of the instance type can then be checked using
TClass.GetInterfaceEntry(IID)
- if it returns non-nil, the class implements the interface.All classes declared in interface sections of units can be retrieved via
TRttiContext.GetTypes
.Writing the actual iteration and extraction code is left as an exercise for the reader.