3

我正在尝试使用TRttiContext.FindType(QualifiedTypeName). 这是我所拥有的:

program MissingRTTI;
{$APPTYPE CONSOLE}
uses System.SysUtils, RTTI, Classes;
type
  TMyClass = class(TObject) end;
var
  rCtx:   TRttiContext;
  rType:  TRttiInstanceType;
begin
  rCtx := TRttiContext.Create();
  rType := rCtx.GetType(TypeInfo(TMyClass)) as TRttiInstanceType;
  if (rType <> nil) then begin
    WriteLn('Type found using TypeInfo');
  end;
  rType := rCtx.FindType(TMyClass.QualifiedClassName) as TRttiInstanceType;
  if (rType <> nil) then begin
    WriteLn('Type found using qualified class name.');
  end;
  ReadLn;
  rCtx.Free();
end.

不幸的是,rCtx.GetType似乎只能找到所需的类型。(我还尝试使用 GetTypes 列出所有类型。所需的类型不会出现在结果数组中。)有人知道如何强制编译器为这种类型发出 RTTI 吗?

4

1 回答 1

7

您对该FindType方法的调用不会返回 Rtti 信息,因为此函数 works only for public types。因此,如果您检查该rType.IsPublicType属性,则返回的值为 false 。

公共类型必须在单元的接口部分中声明(才能被识别为公共)。因此,如果您将TMyClass类定义移动到单元的接口部分,您将能够FindType毫无问题地使用。

于 2012-05-15T20:41:07.900 回答