Delphi TRttiType.GetMethods返回零TRttiMethod实例 [英] Delphi TRttiType.GetMethods return zero TRttiMethod instances

查看:376
本文介绍了Delphi TRttiType.GetMethods返回零TRttiMethod实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近能够使用TRttiContext.FindType使用Robert Loves GetType-workaround (通过显式调用ctx.GetType注册接口,例如RType:= ctx.GetType(TypeInfo(IMyPrettyLittleInterface));)。

I've recently been able to fetch a TRttiType for an interface using TRttiContext.FindType using Robert Loves "GetType"-workaround ("registering" the interface by an explicit call to ctx.GetType, e.g. RType := ctx.GetType(TypeInfo(IMyPrettyLittleInterface));).

一个合乎逻辑的下一步是迭代所述接口的方法。考虑

One logical next step would be to iterate the methods of said interface. Consider

program rtti_sb_1;
{$APPTYPE CONSOLE}
uses
  SysUtils, Rtti, mynamespace in 'mynamespace.pas';
var
  ctx:      TRttiContext;
  RType:    TRttiType;
  Method:   TRttiMethod;
begin
  ctx := TRttiContext.Create;
  RType := ctx.GetType(TypeInfo(IMyPrettyLittleInterface));
  if RType <> nil then begin
    for Method in RType.GetMethods do
      WriteLn(Method.Name);
  end;
  ReadLn;
end.

这一次,我的 mynamespace.pas 看起来像这样:

This time, my mynamespace.pas looks like this:

IMyPrettyLittleInterface = interface
  ['{6F89487E-5BB7-42FC-A760-38DA2329E0C5}']
  procedure SomeProcedure;
end;

不幸的是, RType.GetMethods 返回零-length TArray-instance。有没有人能够重现我的烦恼? (请注意,在我的示例中,我使用TRttiContext.GetType显式获取了TRttiType,而不是解决方法;包含的介绍是为了警告读者可能存在一些有关rtti和接口的未解决问题。)谢谢!

Unfortunately, RType.GetMethods returns a zero-length TArray-instance. Are there anyone able to reproduce my troubles? (Note that in my example I've explicitly fetched the TRttiType using TRttiContext.GetType, not the workaround; the introduction is included to warn readers that there might be some unresolved issues regarding rtti and interfaces.) Thanks!

推荐答案

我只是追踪正在发生的事情,在TRttiInterfaceType.Create第5774行,它说:

I just traced through what's going on, and in TRttiInterfaceType.Create, line 5774, it says:

hasRtti := ReadU16(P);
if hasRtti = $FFFF then
  Exit;

在您继承的接口和IInterface中,HasRtti读取为$ FFFF。因此,显然没有为接口的方法生成RTTI,对于基本接口类型甚至也是如此。我不知道为什么。除了Barry Kelly之外,不知道谁会知道为什么。

And in both your interface, and IInterface which it inherits from, HasRtti reads as $FFFF. So apparently no RTTI is being generated for the interface's methods, and this is even true for the base Interface type. I don't know why. Not sure who would know why, aside from Barry Kelly.

这篇关于Delphi TRttiType.GetMethods返回零TRttiMethod实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆