A few days ago, I saw a post "how to get the name of my unit" in the csdn community. One of them, sdzeng, gave the answer. Inspired by this, I wrote a function to obtain the names of all the ancestor classes of the specified class and their unit names.
// Parameter description:
// Aclass: class of the ancestor class to be obtained
// Aseries: A string list used to store the names of returned ancestor classes and their units
Procedure getinheritanceseries (Aclass: tclass; out Aseries: tstringlist );
VaR
PTD: ptypedata;
PTI: ptypeinfo;
Pclass: tclass;
Begin
Aseries. Clear;
Pclass: = tbutton; if not Pclass. inheritsfrom (tpersistent) Then exit;
PTD: = gettypedata (Pclass. classinfo );
Aseries. Add (format ('% s (% s)', [Pclass. classname, PTD. unitname]);
Repeat
PTD: = gettypedata (Pclass. classinfo );
PTI: = PTD. parentinfo ^;
Aseries. insert (0, format ('% s (% s)', [PTI ^. Name, PTD. unitname]);
Pclass: = Pclass. classparent;
Until not Pclass. inheritsfrom (tpersistent );
End;
Example
// Obtain all the ancestor classes of the tbutton class and their unit names
VaR
SL: tstringlist;
Begin
SL: = tstringlist. Create;
Try
Getinheritanceseries (tbutton, SL );
Memo1.lines. addstrings (SL );
Finally
SL. Free;
End;
End;
The returned results are as follows:
Tobject (classes)
Tpersistent (classes)
Tcomponent (controls)
Tcontrol (controls)
Twincontrol (stdctrls)
Tbuttoncontrol (stdctrls)
Tbutton (stdctrls)
Additional instructions
Note that the use of gettypedata (Pclass. classinfo); in the above Code must ensure that this class can be used only from the derived class of the tpersistent class; otherwise, an address access error will be thrown.