Delphi 2010 RTTI - RttiContext.FindType

With RttiContext.FindType('Classes.TStringList') I get RttiType from TStringList without any problems. But with RttiContext.FindType('MyUnit.TMyClass') I always get nil (of course, MyUnit in the uses section). Why? What's 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 !! ... 
+3
source share
2 answers

The class probably did not include the delphi linker in the final executable. A quick try might be the following:

  • Declare a static method for your class. This method should be empty, simple begin end .
  • Call this static method in the initialization section of this device.
  • Make sure the device is listed in your project somewhere.
  • You should now see a class with TRttiContext.FindType .
+8
source

This may be a few things. It's hard to say without seeing your code, but here are a few tips to look out for. Is TMyClass publicly available in an interface section? Is RTTI generation enabled for this device? Is MyUnit a package not yet loaded?

+1
source

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


All Articles