Is there at way, at runtime, to find all classes that descend from a particular base class?
For example, pretend there is a class:
TLocalization = class(TObject)
...
public
function GetLanguageName: string;
end;
or pretend there is a class:
TTestCase = class(TObject)
...
public
procedure Run; virtual;
end;
or pretend there is a class:
TPlugIn = class(TObject)
...
public
procedure Execute; virtual;
end;
or pretend there is a class:
TTheClassImInterestedIn = class(TObject)
...
public
procedure Something;
end;
At runtime i want to find all classes that descend from TTestCase
so that i may do stuff with them.
Can the RTTI be queried for such information?
Alternatively: Is there a way in Delphi to walk every class? i can then simply call:
RunClass: TClass;
if (RunClass is TTestCase) then
begin
TTestCase(RunClass).Something;
end;
Well, yes, there is a way, but you're not going to like it. (Appearantly, I need a disclaimer like this, to prevent my otherwise perfectly helpfull comment getting downvoted by the oh-so knowledgable, but not so forgiving 'senior' SO members.)
FYI : The following description is a high-level overview of a piece of code I actually wrote when Delphi 5 was the latest & greatest. Since then, that code was ported over to newer Delphi versions (currently up until Delphi 2010) and still works!
For starters, you need to know that a class is nothing more than a combination of a VMT and the accompanying functions (and maybe some type-info, depending on compiler-version and -settings). As you probably know, a class - as identified by the type TClass - is just a pointer to the memory address of that classes' VMT. In other words : If you known the address of the VMT of a class, that's the TClass pointer as well.
With that piece of knowledge stuck firmly in your mind, you can actually scan your executable memory, and for each address test if it 'looks like' a VMT. All addresses that seem to be a VMT can than be added to a list, resulting in a complete overview of all classes contained in your executable! (Actually, this even gives you access to classes declared solely in the implementation-section of a unit, and classes linked-in from components & libraries that are distributed as binaries!)
Sure, there's a risk that some addresses seem to be a valid VMT, but are actually some random other data (or code) - but with the tests I've come up with, this has never happened to me yet (in about 6 years running this code in more than ten actively maintained applications).
So here's the checks you should do (in this exact order!) :
If all these checks hold, the test-address is a valid VMT (as far as I'm concerned) and can be added to the list.
Good luck implementing this all, it took me about a week to get this right.
Please tell how it works out for you. Cheers!
Ian, as Mason says the
TRttiContext.GetTypes
function get the list of all RTTI objects that provide type information . but this function was introduced in Delphi 2010.As workaround you can inherit your base class from the
TPersistent
class and then register manually every class using theRegisterClass
function (i know wich this is annoying).then using the
TClassFinder
object you can retrieve all the registered classes.see this sample
UPDATE
I'm sorry, but apparently the
TClassFinder
class was introduced in Delphi 6It can be done with RTTI, but not in Delphi 5. In order to find all classes that match a certain criteria, you first need to be able to find all classes, and the RTTI APIs necessary to do that were introduced in Delphi 2010. You'd do it something like this: