Why doesn't FindType get RTTI when GetType succeeds?

I am trying to get an object using TRttiContext.FindType(QualifiedTypeName) . Here is what I have:

 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. 

Unfortunately, only rCtx.GetType seems to be looking for the right type. (I also tried to list all types using GetTypes. The desired type does not appear in the resulting array.) Does anyone know how to get the compiler to emit RTTI for this type?

+3
source share
1 answer

Your call to the FindType method FindType not return Rtti information, since this function works only for public types . Therefore, if you check the rType.IsPublicType property, the return value will be false.

Public types must be declared in the section interface section of the section (to be recognized as public). Therefore, if you move the TMyClass class TMyClass to the interface part of the device, you can use FindType without any problems.

+7
source

Source: https://habr.com/ru/post/915892/


All Articles