3
votes

With RttiContext.FindType('Classes.TStringList') I get RttiType of TStringList with no problem. But with RttiContext.FindType('MyUnit.TMyClass') I always get nil (of course MyUnit is in uses clause). Why, what is wrong?

Example:

unit MyUnit; 
interface 
uses 
  Classes; 
type 
  TMyClass = class(TStringList) 
  end; 
implementation 
end. 

Main unit: 
... 
uses 
  MyUnit,
... 
var 
  oCont: TRttiContext; 
  oType: TRttiType; 
begin 
  oCont := TRttiContext.Create; 
  try 
    oType := oCont.FindType('MyUnit.TMyClass'); <== oType = nil !! 
... 
2

2 Answers

8
votes

Probably the class has not included by the delphi linker in the final executable. A fast try can be the following:

  1. Declare a static method on your class. This method should be an empty method, a simple begin end.
  2. Call this static method in the initialization section of this unit.
  3. Ensure that the unit is referenced in your project somewhere.
  4. Now you should see the class with TRttiContext.FindType.
1
votes

It could be a handful of things. Hard to say without seeing your code, but here are a few suggestions to look at. Is TMyClass a public type in the interface section? Is RTTI generation turned on for that unit? Is MyUnit in a package that hasn't been loaded yet?