我正在尝试使用来抓住一个物体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 吗?
您致电FindType http://docwiki.embarcadero.com/Libraries/en/System.Rtti.TRttiContext.FindType方法不返回 Rtti 信息,因为此函数works only for public types
。所以如果你检查rType.IsPublicType http://docwiki.embarcadero.com/VCL/2010/en/Rtti.TRttiType.IsPublicType属性返回的值为 false 。
公共类型必须在单元的接口部分中声明(以被识别为公共)。所以如果你移动TMyClass
类定义到单元的接口部分,您将能够使用FindType
没有什么问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)