枚举实现给定接口的所有 Delphi 类?

发布于 2024-08-20 16:21:59 字数 68 浏览 4 评论 0原文

通过 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 技术交流群。

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

发布评论

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

评论(1

梦幻的心爱 2024-08-27 16:21:59

从技术上讲是可以的,只要接口有 GUID 并且您关心的类位于单元的接口部分中。

RTTI 单元(尚)没有用于描述类实现的所有接口的 API,但数据位于类型信息中。

接口类型的 GUID 可以从此表达式获取:

GetTypeData(TypeInfo(ITheInterface))^.Guid

或者:

ctx: TRttiContext;
// ...
(ctx.GetType(TypeInfo(ITheInterface)) as TRttiInterfaceType).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:

GetTypeData(TypeInfo(ITheInterface))^.Guid

or:

ctx: TRttiContext;
// ...
(ctx.GetType(TypeInfo(ITheInterface)) as TRttiInterfaceType).GUID

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.

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