The reflection mechanism in Java allows us to get information about the runtime class during run time, so is there such a feature in Delphi? The answer is that the mechanism to implement this function in Delphi is called Rtti, the nonsense is less, first to a demo:
1. First define a demo class, note that this class must be tpersistent as the base class, the code is as follows:
Delphi Code
- Unit Unit2;
- Interface
- {Tdemo}
- Uses Classes;
- Type
- Tdemo = Class (Tpersistent)
- Private
- Protected
- Public
- fname:string;
- Fage:integer;
- Constructor Create;
- destructor Destroy; Override
- Published
- Property name:string read FName write FName;
- Property Age:integer read Fage write Fage;
- End
- Implementation
- {Tdemo}
- Constructor Tdemo.create;
- Begin
- FName: = ' Peirenlei ';
- End
- destructor Tdemo.destroy;
- Begin
- inherited;
- End
- End.
2. Next, create an instance of this class:
Delphi Code
- Ademo: = tdemo.create;
- Ademo.name: = ' Peirenlei ';
- Ademo.age: = 0;
3. Get the list of properties for this instance:
Delphi Code
- Var
- Aproplist:pproplist;
- Atypeinfo:ptypeinfo;
- Aclasdateinfo:ptypedata;
- I:integer;
- Begin
- Atypeinfo: = Ademo.classinfo;
- Aclasdateinfo: = Gettypedata (Atypeinfo);
- If Aclasdateinfo.propcount <> 0 Then
- Begin
- Allocate space
- Getmem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
- Try
- Getpropinfos (ademo.classinfo,aproplist); Get a list of properties for a class
- For I: = 0 to Aclasdateinfo.propcount- 1 do
- Begin
- If aproplist[i]^. proptype^. Kind <> Tkmethod Then
- Mmo. Lines.add (Format ('%s:%s ', [aproplist[i]^. name,aproplist[i]^. proptype^. Name]));
- End
- Finally
- Freemem (aproplist,sizeof (aproplist) *aclasdateinfo.propcount);
- End
- End
- End
Output:
Delphi Code
- Name:string
- Age:integer
3. Get a list of methods for the class:
Delphi Code
- Var
- Aproplist:pproplist;
- Atypeinfo:ptypeinfo;
- Aclasdateinfo:ptypedata;
- I, Numpro:integer;
- Begin
- Atypeinfo: = Ademo.classinfo;
- Aclasdateinfo: = Gettypedata (Atypeinfo);
- If Aclasdateinfo.propcount <> 0 Then
- Begin
- Allocate space
- Getmem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
- Try
- Numpro: = Getproplist (ademo.classinfo,[tkmethod],aproplist);
- If Numpro <> 0 Then
- Begin
- Mmo. Lines.add ('-----events-------');
- For I: = 0 to Numpro- 1 do//Get a list of event properties
- Begin
- Mmo. Lines.add (Format ('%s:%s ', [aproplist[i]^. name,aproplist[i]^. proptype^. Name]));
- End
- End
- Finally
- Freemem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
- End
- End
- End
4. Get the property and property values for the instance class:
Delphi Code
- Var
- Aproperinfo:ppropinfo;
- avalue:string;
- Begin
- Avalue: = GetPropValue (Ademo,' Name ');
- Mmo. Lines.add (' name= ' +avalue);
- Avalue: = GetPropValue (Ademo,' age ');
- Mmo. Lines.add (' age= ' +avalue);
- Setpropvalue (Ademo,' Name ',' zengzhen ');
- Setpropvalue (Ademo,' age ',100);
- Avalue: = GetPropValue (Ademo,' Name ');
- Mmo. Lines.add (' name= ' +avalue);
- Avalue: = GetPropValue (Ademo,' age ');
- Mmo. Lines.add (' age= ' +avalue);
- End
Output:
Delphi Code
- Name=peirenlei
- age=0
- Name=zengzhen
- Age=
http://peirenlei.iteye.com/blog/378465
A preliminary examination of Rtti in Delpi