Per schedule-rtti reads class read class structure and class forced conversion

Source: Internet
Author: User

In any data type, rtti has a corresponding class for obtaining information. The ordered type corresponds to trttiordinaltype.

Download rtti3 in demo

Code: </P> <p> // in any data type, rtti has the corresponding class for obtaining information. The ordered type corresponds to trttiordinaltype. <br/> unit unit1; </P> <p> interface </P> <p> uses <br/> Windows, messages, sysutils, variants, classes, graphics, controls, forms, <br/> dialogs, stdctrls; </P> <p> type <br/> tform1 = Class (tform) <br/> mmo1: tmemo; <br/> btn1: tbutton; <br/> btn2: tbutton; <br/> btn3: tbutton; <br/> procedure btn1click (Sender: tobject ); <br/> procedure btn2click (Sender: tobject); <br/> procedure btn3click (Sender: tobject ); <br/> private <br/> {private Declarations} <br/> Public <br/> {public declarations} <br/> end; </P> <p> var <br/> form1: tform1; </P> <p> Implementation </P> <p> {$ R *. DFM} <br/> uses <br/> rtti, typinfo; </P> <p> procedure tform1.btn1click (Sender: tobject ); <br/> var <br/> T: trttiordinaltype; <br/> begin <br/> mmo1.clear; </P> <p> // obtain the type information object from the type name <br/> T: = trtticontext. create. getType (typeinfo (byte) as trttiordinaltype; <br/> mmo1.lines. add (format ('% s-% s', [T. name, T. qualifiedname]); <br/> mmo1.lines. add (format ('size: % d', [T. typesize]); <br/> mmo1.lines. add ('qualifiedname: '+ T. qualifiedname); <br/> mmo1.lines. add (format ('min, Max: % d, % d', [T. minvalue, T. maxvalue]); <br/> mmo1.lines. add (emptystr); // empty string </P> <p> // you can use the asordinal method to replace the preceding as trttiordinaltype. <br/> T: = trtticontext. create. getType (typeinfo (Word )). asordinal; <br/> mmo1.lines. add (format ('% s: % s', [T. name, T. qualifiedname]); <br/> mmo1.lines. add (format ('size: % d', [T. typesize]); <br/> mmo1.lines. add (format ('min, Max: % d, % d', [T. minvalue, T. maxvalue]); <br/> mmo1.lines. add (emptystr); </P> <p> // You can also force the conversion directly <br/> T: = trttiordinaltype (trtticontext. create. getType (typeinfo (integer); <br/> mmo1.lines. add (format ('% s: % s', [T. name, T. qualifiedname]); <br/> mmo1.lines. add (format ('size: % d', [T. typesize]); <br/> mmo1.lines. add (format ('min, Max: % d, % d', [T. minvalue, T. maxvalue]); <br/> mmo1.lines. add (emptystr); <br/> end; </P> <p> // The following uses tpoint as an example to read the structure information using trttirecordtype. <br/> procedure tform1.btn2click (Sender: tobject); <br/> var <br/> T: trttirecordtype; <br/> F: trttifield; <br/> begin <br/> mmo1.clear; <br/> T: = trtticontext. create. getType (typeinfo (tpoint )). asrecord; <br/> mmo1.lines. add (T. qualifiedname); <br/> mmo1.lines. add (format ('size: % d', [T. typesize]); <br/> mmo1.lines. add (emptystr); </P> <p> mmo1.lines. add (format ('number of fields: % d', [length (T. getfields)]); <br/> mmo1.lines. add (format ('number of Methods: % d', [length (T. getmethods)]); <br/> mmo1.lines. add (format ('number of attributes: % d', [length (T. getproperties)]); <br/> mmo1.lines. add (emptystr); </P> <p> mmo1.lines. add ('all fields: '); <br/> for F in T. getfields do mmo1.lines. add (F. tostring); <br/> end; </P> <p> procedure tform1.btn3click (Sender: tobject); <br/> var <br/> MS: tarray <trttimethod>; <br/> M: trttimethod; <br/> MPs: tarray <trttiparameter >;< br/> MP: trttiparameter; <br/> begin <br/> mmo1.clear; </P> <p> {obtain the method set first. The tbutton class is used here.} <br/> MS: = trtticontext. create. getType (tbutton ). getmethods; <br/> for m in ms do <br/> begin <br/> {method name} <br/> mmo1.lines. add ('method name: '+ M. name); </P> <p> {method type: Proceedure, function, etc.} <br/> mmo1.lines. add ('method type: '+ getenumname (typeinfo (tmethodkind), ord (M. methodkind); </P> <p> {Return Value Type of the method} <br/> if assigned (M. returntype) Then <br/> mmo1.lines. add ('Return value: '+ getenumname (typeinfo (ttypekind), ord (M. returntype. typekind); </P> <p> {method parameter list} <br/> MPs: = m. getparameters; <br/> If length (MPS)> 0 then <br/> begin <br/> mmo1.lines. add ('parameter: '); <br/> for MP in MPs do mmo1.lines. add (MP. tostring); <br/> // you can also use MP. paramtype: obtain the data type of the parameter <br/> // You can also use the MP. flags obtains parameter modifiers (such as VAR and const) <br/> end; </P> <p> mmo1.lines. add (emptystr); <br/> end; </P> <p> end. </P> <p> form part: </P> <p> Object form1: tform1 <br/> left = 0 <br/> Top = 0 <br/> caption = #24858 #20154 #31508 #35760 '-http://www.foolcode.com' <br/> clientheight = 285 <br/> clientwidth = 444 <br/> color = clbtnface <br/> font. charset = default_charset <br/> font. color = clwindowtext <br/> font. height =-11 <br/> font. name = 'tahoma '<br/> font. style = [] <br/> oldcreateorder = false <br/> pixelsperinch = 96 <br/> textheight = 13 <br/> Object mmo1: tmemo <br/> left = 0 <br/> Top = 8 <br/> width = 436 <br/> Height = 201 <br/> imename = #20013 #25991' ('#31616 #20307 ') -'#25628 #29399 #25340 #38899 #36755 #20837 #27861 <br/> lines. strings = (<br/> 'mmo1') <br/> taborder = 0 <br/> end <br/> Object btn1: tbutton <br/> left = 40 <br/> Top = 240 <br/> width = 75 <br/> Height = 25 <br/> caption = 'trttiordinaltype' <br /> taborder = 1 <br/> onclick = btn1click <br/> end <br/> Object btn2: tbutton <br/> left = 160 <br/> Top = 240 <br/> width = 75 <br/> Height = 25 <br/> caption = 'trttirecordtype' <br /> taborder = 2 <br/> onclick = btn2click <br/> end <br/> Object btn3: tbutton <br/> left = 304 <br/> Top = 240 <br/> width = 75 <br/> Height = 25 <br/> caption = 'getnumname' <br /> taborder = 3 <br/> onclick = btn3click <br/> 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.