Rtti Unit (1) obtains the list of methods, attributes, and fields of a class.

Source: Internet
Author: User

Through the trtticontext (record) of the rtti unit, you can conveniently obtain the list of methods, attributes, and fields of the class.

Unit unit1;
 
Interface
 
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
 
Type
Tform1 = Class (tform)
Memo1: tmemo;
Button1: tbutton;
Button2: tbutton;
Button3: tbutton;
Button4: tbutton;
Button5: tbutton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Procedure button3click (Sender: tobject );
Procedure button4click (Sender: tobject );
Procedure button5click (Sender: tobject );
End;
 
VaR
Form1: tform1;
 
Implementation
 
{$ R *. DFM}
 
Uses rtti;
 
// Trtticontext. gettypes
Procedure tform1.button1click (Sender: tobject );
VaR
CTX: trtticontext;
T: trttitype;
Begin
Memo1.clear;
For T in CTX. gettypes do memo1.lines. Add (T. Name );
End;
 
// Obtain the tbutton Class Method
Procedure tform1.button2click (Sender: tobject );
VaR
CTX: trtticontext;
T: trttitype;
M: trttimethod;
Begin
Memo1.clear;
T: = CTX. GetType (tbutton );
// For m in T. getmethods do memo1.lines. Add (M. Name );
For m in T. getmethods do memo1.lines. Add (M. tostring );
End;
 
// Obtain attributes of the tbutton class
Procedure tform1.button3click (Sender: tobject );
VaR
CTX: trtticontext;
T: trttitype;
P: trttiproperty;
Begin
Memo1.clear;
T: = CTX. GetType (tbutton );
// For P in T. getproperties do memo1.lines. Add (P. Name );
For P in T. getproperties do memo1.lines. Add (P. tostring );
End;
 
// Obtain fields of the tbutton class
Procedure tform1.button4click (Sender: tobject );
VaR
CTX: trtticontext;
T: trttitype;
F: trttifield;
Begin
Memo1.clear;
T: = CTX. GetType (tbutton );
// For F in T. getfields do memo1.lines. Add (F. Name );
For f in T. getfields do memo1.lines. Add (F. tostring );
End;
 
// Obtain the method set, attribute set, and field set for the tbutton class.
Procedure tform1.button5click (Sender: tobject );
VaR
CTX: trtticontext;
T: trttitype;
MS: tarray <trttimethod>;
PS: tarray <trttiproperty>;
FS: tarray <trttifield>;
Begin
Memo1.clear;
T: = CTX. GetType (tbutton );
 
MS: = T. getmethods;
PS: = T. getproperties;
FS: = T. getfields;
 
Memo1.lines. Add (format ('% S Class Total % d methods', [T. Name, length (MS)]);
Memo1.lines. Add (format ('% s class has % d attributes in total', [T. Name, length (PS)]);
Memo1.lines. Add (format ('% S Class Total % d fields', [T. Name, length (FS)]);
End;
 
End.

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.