问题
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 !!
...
回答1:
Probably the class has not included by the delphi linker in the final executable. A fast try can be the following:
- Declare a static method on your class. This method should be an empty method, a simple
begin end
. - Call this static method in the initialization section of this unit.
- Ensure that the unit is referenced in your project somewhere.
- Now you should see the class with
TRttiContext.FindType
.
回答2:
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?
来源:https://stackoverflow.com/questions/3460382/delphi-2010-rtti-rtticontext-findtype