Branko
Branko

Reputation: 1468

Delphi 2010 RTTI - RttiContext.FindType

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 !! 
... 

Upvotes: 4

Views: 2873

Answers (2)

Daniele Teti
Daniele Teti

Reputation: 1784

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.

Upvotes: 9

Mason Wheeler
Mason Wheeler

Reputation: 84650

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?

Upvotes: 1

Related Questions