In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use RTTI in Delphi. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Summary
Runtime type information (RTTI) is a language feature that enables applications to get information about objects at run time.
RTTI is the key to the integration of Delphi components into IDE. It is not only a purely academic process in IDE.
Because objects inherit from TObject, objects contain a pointer to their RTTI and several built-in methods. The following table lists some of the methods of TObject that can be used to get information about an object instance.
Part one: about as and is
Object Pascal provides two operators, as and is, that allow you to compare and cast objects through RTTI.
The keyword as is a new form of type conversion. It can cast a grass-roots object type into its derived class, and an exception will be generated if the conversion is illegal. Suppose there is a process that wants it to be able to pass any type of object, it should be defined like this:
Procedure Foo (AnObject: Tobject)
If you want to operate on AnObject during this process, you need to convert it to a derived object. Suppose you think of AnObject as a TEdit derived type and want to change the text it contains, with the following code: (AnObject as Tedit) .text: = 'wudi_1982'
The comparison operator can be used to determine whether two objects are compatible types, and the is operator can be used to compare an unknown object with a known type or instance to determine the properties and behavior of the unknown object. For example, before casting (AnObject), determine whether (AnObject and TEdit are pointer compatible:
If (AnObject is Tedit) then
Tedit (AnObjject) .text: = 'wudi_1982'
Note that as is no longer used for casting in this example, because it uses RTTI a lot, and because it has been determined on the first line that Foo is TEdit, which can be optimized by pointer conversion on line 2.
Procedure TForm1.ClearEdit (Acontrl: TWinControl)
Var
I: integer
Begin
For I: = 0 to Acontrl.ControlCount-1 do
Begin
If Acontrl.Controls [i] is TEdit then
((Acontrl.Controls [I]) as TEdit) .Text: =''
If Acontrl.Controls [i] is TCustomControl then
ClearEdit ((Acontrl.Controls [I] as TCustomControl))
End
End
Part II: RTTI
RTTI has been mentioned many times above, but I don't seem to see RTTI. So how does RTTI express itself? You will find that RTTI is useful to you in at least two places. The first place is DELPHI's IDE, which was mentioned earlier. Through RTTI,IDE, you will know everything about the objects and components you are using. Actually, it's not just RTTI, but for this discussion, let's just talk about RTTI. In fact, the above as,is operations all indirectly use RTTI.
Let's use an example to demonstrate it. While viewing this example, it is recommended that you take a look at the contents of typinfo.pas (DELPHI installation directory / source/rtl/common/TypInfo.pas)
The following example is mainly divided into two parts, the top half of the interface, mainly demonstrates the use of rtti to display the type of information selected by the user. (there are 3 TListBox)
The following section mainly completes the assignment of properties to the control through configuration information through RTTI, which demonstrates the assignment of text types and event types.
The form file is as follows: the code is as follows:
Object Form1: TForm1
Left = 150
Top = 161,
Width = 639
Height = 372,
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height =-11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
Object Panel1: TPanel
Left = 0
Top = 0
Width = 631
Height = 185,
Align = alTop
TabOrder = 0
Object GroupBox1: TGroupBox
Left = 1
Top = 1
Width = 185,
Height = 183,
Align = alLeft
Caption = 'Select the type of information you want to view here'
TabOrder = 0
Object ListBox1: TListBox
Left = 2
Top = 15
Width = 181,
Height = 166,
Align = alClient
ItemHeight = 13
TabOrder = 0
OnClick = ListBox1Click
End
End
Object GroupBox2: TGroupBox
Left = 368
Top = 1
Width = 262,
Height = 183,
Align = alRight
Caption = 'attribute information'
TabOrder = 1
Object ListBox3: TListBox
Left = 2
Top = 15
Width = 258
Height = 166,
Align = alClient
ItemHeight = 13
TabOrder = 0
End
End
Object GroupBox3: TGroupBox
Left = 186
Top = 1
Width = 182
Height = 183,
Align = alClient
Caption = 'basic information'
TabOrder = 2
Object ListBox2: TListBox
Left = 2
Top = 15
Width = 178,
Height = 166,
Align = alClient
ItemHeight = 13
TabOrder = 0
End
End
End
Object TPanel
Left = 0
Top = 185,
Width = 631
Height = 157,
Align = alClient
TabOrder = 1
Object Panel2: TPanel
Left = 1
Top = 1
Width = 230
Height = 155
Align = alLeft
TabOrder = 0
Object Label2: TLabel
Left = 10
Top = 8
Width = 84
Height = 13
Caption = 'name of the control to be modified'
End
Object Label3: TLabel
Left = 8
Top = 32
Width = 72
Height = 13
Caption = 'modified attribute name'
End
Object Label4: TLabel
Left = 8
Top = 64
Width = 72
Height = 13
Caption = 'modify property to'
End
Object edComName: TEdit
Left = 104
Top = 5
Width = 78
Height = 21
TabOrder = 0
Text = 'label1'
End
Object edPproName: TEdit
Left = 104
Top = 32
Width = 81
Height = 21
TabOrder = 1
Text = 'caption'
End
Object edValue: TEdit
Left = 104
Top = 56
Width = 81
Height = 21
TabOrder = 2
Text = '12345'
End
Object btnInit: TButton
Left = 8
Top = 104
Width = 75
Height = 25
Caption = 'initialization'
TabOrder = 3
OnClick = btnInitClick
End
Object btnModify: TButton
Left = 104
Top = 104
Width = 75
Height = 25
Caption = 'modify'
TabOrder = 4
OnClick = btnModifyClick
End
End
Object Panel3: TPanel
Left = 231
Top = 1
Width = 399
Height = 155
Align = alClient
TabOrder = 1
Object GroupBox4: TGroupBox
Left = 1
Top = 1
Width = 397
Height = 153
Align = alClient
Caption = 'modified control'
TabOrder = 0
Object Label1: TLabel
Left = 16
Top = 32
Width = 28
Height = 13
Caption = 'label1'
End
Object BitBtn1: TBitBtn
Left = 8
Top = 64
Width = 75
Height = 25
Caption = 'BitBtn1'
TabOrder = 0
End
End
End
End
End
Unit main
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls
Forms,Dialogs,typinfo, StdCtrls, ExtCtrls, Buttons
Type
InsertCom = record
Name: string; / / the name of the component to modify the property
PproName: string;// wants to modify the property name of the control
MethodName: string;// wants to modify the event name that or adds to the control
Text: string; / / attribute value. Here, the value of type string is modified.
End
TForm1 = class (TForm)
Panel1: TPanel
GroupBox1: TGroupBox
ListBox1: TListBox
GroupBox2: TGroupBox
GroupBox3: TGroupBox
ListBox2: TListBox
ListBox3: TListBox
Panel2: TPanel
EdComName: TEdit
Label2: TLabel
Label3: TLabel
EdPproName: TEdit
Label4: TLabel
EdValue: TEdit
Panel3: TPanel
BtnInit: TButton
BtnModify: TButton
GroupBox4: TGroupBox
Label1: TLabel
BitBtn1: TBitBtn
Procedure FormCreate (Sender: TObject)
Procedure ListBox1Click (Sender: TObject)
Procedure btnInitClick (Sender: TObject)
Procedure btnModifyClick (Sender: TObject)
Private
TestCom: InsertCom
Procedure MyClick (Sender: TObject); / / add an onclick event to the control
Public
{Public declarations}
End
Var
Form1: TForm1
Implementation
{$R * .dfm}
Function CreateClass (const AClassName: string): TObject;// is generated by name
Var
Tm: TObject
T: TFormClass
Begin
T: = TFormClass (FindClass (AClassName))
Tm: = t.Create (nil)
Result: = tm
End
Procedure GetBaseClassInfo (AClass: TObject;AStrings: TStrings); / / get basic information about the type
Var
ClassTypeInfo: PTypeInfo
ClassDataInfo: PTypeData
Begin
ClassTypeInfo: = AClass.ClassInfo
ClassDataInfo: = GetTypeData (classTypeInfo)
With AStrings do
Begin
Add (Format ('name is:% slots, [classTypeInfo.Name]))
Add (format ('type kind is:% slots, [GetEnumName (TypeInfo (TTypeKind), integer (classTypeInfo.Kind)])
Add (Format ('in:% slots, [ClassDataInfo.UnitName]))
End
End
Procedure GetBaseClassPro (AClass: TObject;Astrings: TStrings); / / get attribute information
Var
NumPro: number of integer; / / used to record event attributes
Pplst: PPropList; / / store attribute list
Classtypeinfo: PTypeInfo
ClassDataInfo: PTypeData
I: integer
Begin
Classtypeinfo: = AClass.ClassInfo
ClassDataInfo: = GetTypeData (Classtypeinfo)
If classDataInfo.PropCount 0 then
Begin
/ / allocate space
GetMem (Pplst,sizeof (PpropInfo) * classDataInfo.PropCount)
Try
/ / get attribute information to pplst
GetPropInfos (AClass.ClassInfo,Pplst)
For I: = 0 to classDataInfo.PropCount-1 do
Begin
If Pplst [I] ^ .PropType ^ .Kind tkMethod then
/ / event attributes are filtered out here
Astrings.Add (Format ('% slug% slots, [Pplst [I] ^ .Name, Pplst [I] ^ .PropType.Name]))
End
/ / get event attributes
NumPro: = GetPropList (AClass.ClassInfo, [tkMethod], Pplst)
If NumPro 0 then
Begin
/ / add some flags to the list
Astrings.Add ('')
Astrings.Add ('- EVENT-')
Astrings.Add ('')
For I: = 0 to NumPro-1 do / / get a list of event attributes
Astrings.Add (Format ('% slug% slots, [Pplst [I] ^ .Name, Pplst [I] ^ .PropType.Name]))
End
Finally
FreeMem (Pplst,sizeof (PpropInfo) * classDataInfo.PropCount)
End
End
End
Procedure TForm1.btnInitClick (Sender: TObject)
Begin
/ / modify the caption attribute of label1 to 12345
TestCom.Name: = edComName.Text
TestCom.PproName: = edPproName.Text
TestCom.text: = edValue.Text
TestCom.MethodName: = 'OnClick'
BtnModify.Enabled: = true
End
Procedure TForm1.btnModifyClick (Sender: TObject)
Var
Pp: PPropInfo
Obj: TComponent
A: TMethod
Tm: TNotifyEvent
Begin
Obj: = FindComponent (TestCom.Name); / / find this control by name
If not Assigned (obj) then exit; / / exit if not
/ / get the property information of the specified control through getPropInfo. Note that only those properties that have been disclosed can be obtained here.
Pp: = GetPropInfo (obj.ClassInfo,TestCom.PproName)
If Assigned (pp) then
Begin
/ / judge whether the type is string type according to kind
Case pp^ .PropType ^ .Kind of
/ / setStrProp is used to assign values to attributes of type string. For the assignment of types, please refer to TypInfo.pas
TkString,tkLString,tkWString: SetStrProp (obj,TestCom.PproName,TestCom.text)
End
/ / add an onClick event to the control to be modified
Pp: = GetPropInfo (obj.ClassInfo,TestCom.MethodName)
If Assigned (pp) then
Begin
If p ^ .PropType ^ .Kind = tkMethod then
Begin
Tm: = MyClick
/ / the code of Tmethod is the function address, which can also be obtained through the MethodAddress method.
A.Code: = @ tm
A.Data: = Self
/ / A pair of time assignments
SetMethodProp (obj,TestCom.MethodName,a)
End
End
End
End
Procedure TForm1.FormCreate (Sender: TObject)
Begin
BtnModify.Enabled: = false
/ / add some type class names to listbox1
With ListBox1.Items do
Begin
Add ('TApplication')
Add ('TEdit')
Add ('TButton')
Add ('Tmemo')
Add ('TForm')
End
End
Procedure TForm1.ListBox1Click (Sender: TObject)
Var
T: TObject
Begin
/ / when a type is selected in the type list and clicked with the mouse, its attribute information and basic information are obtained respectively.
ListBox2.Clear
ListBox3.Clear
T: = CreateClass (ListBox1.Items [ListBox1.ItemIndex])
Try
GetBaseClassInfo (tMagneListBox2.Items)
GetBaseClassPro (tMagneListBox3.Items)
Finally
T.Free
End
End
Procedure TForm1.MyClick (Sender: TObject)
Begin
/ / A method to add to a specified control
ShowMessage ('wudi_1982')
End
Initialization
/ / register at initialization
RegisterClasses ([TApplication,TButton,TEdit,TMemo,TForm])
After reading the above, do you have any further understanding of how to use RTTI in Delphi? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.