【问题标题】:Why does FindType fail to get RTTI when GetType succeeds?为什么GetType成功时FindType获取不到RTTI?
【发布时间】:2012-05-15 11:57:37
【问题描述】:

我正在尝试使用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 吗?

【问题讨论】:

    标签: delphi delphi-xe2 rtti


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2015-06-13
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多