Get the names of certain ancestor classes and their units in Delphi

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.