A preliminary examination of Rtti in Delpi

Source: Internet
Author: User

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
  1. Unit Unit2;
  2. Interface
  3. {Tdemo}
  4. Uses Classes;
  5. Type
  6. Tdemo = Class (Tpersistent)
  7. Private
  8. Protected
  9. Public
  10. fname:string;
  11. Fage:integer;
  12. Constructor Create;
  13. destructor Destroy; Override
  14. Published
  15. Property name:string read FName write FName;
  16. Property Age:integer read Fage write Fage;
  17. End
  18. Implementation
  19. {Tdemo}
  20. Constructor Tdemo.create;
  21. Begin
  22. FName: = ' Peirenlei ';
  23. End
  24. destructor Tdemo.destroy;
  25. Begin
  26. inherited;
  27. End
  28. End.

2. Next, create an instance of this class:

Delphi Code
    1. Ademo: = tdemo.create;
    2. Ademo.name: = ' Peirenlei ';
    3. Ademo.age: = 0;

3. Get the list of properties for this instance:

Delphi Code
  1. Var
  2. Aproplist:pproplist;
  3. Atypeinfo:ptypeinfo;
  4. Aclasdateinfo:ptypedata;
  5. I:integer;
  6. Begin
  7. Atypeinfo: = Ademo.classinfo;
  8. Aclasdateinfo: = Gettypedata (Atypeinfo);
  9. If Aclasdateinfo.propcount <> 0 Then
  10. Begin
  11. Allocate space
  12. Getmem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
  13. Try
  14. Getpropinfos (ademo.classinfo,aproplist); Get a list of properties for a class
  15. For I: = 0 to Aclasdateinfo.propcount- 1 do
  16. Begin
  17. If aproplist[i]^. proptype^. Kind <> Tkmethod Then
  18. Mmo. Lines.add (Format ('%s:%s ', [aproplist[i]^. name,aproplist[i]^. proptype^.          Name]));
  19. End
  20. Finally
  21. Freemem (aproplist,sizeof (aproplist) *aclasdateinfo.propcount);
  22. End
  23. End
  24. End

Output:

Delphi Code
    1. Name:string
    2. Age:integer

3. Get a list of methods for the class:

Delphi Code
  1. Var
  2. Aproplist:pproplist;
  3. Atypeinfo:ptypeinfo;
  4. Aclasdateinfo:ptypedata;
  5. I, Numpro:integer;
  6. Begin
  7. Atypeinfo: = Ademo.classinfo;
  8. Aclasdateinfo: = Gettypedata (Atypeinfo);
  9. If Aclasdateinfo.propcount <> 0 Then
  10. Begin
  11. Allocate space
  12. Getmem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
  13. Try
  14. Numpro: = Getproplist (ademo.classinfo,[tkmethod],aproplist);
  15. If Numpro <> 0 Then
  16. Begin
  17. Mmo.  Lines.add ('-----events-------');
  18. For I: = 0 to Numpro- 1 do//Get a list of event properties
  19. Begin
  20. Mmo. Lines.add (Format ('%s:%s ', [aproplist[i]^. name,aproplist[i]^. proptype^.  Name]));
  21. End
  22. End
  23. Finally
  24. Freemem (aproplist,sizeof (ppropinfo) *aclasdateinfo.propcount);
  25. End
  26. End
  27. End

4. Get the property and property values for the instance class:

Delphi Code
  1. Var
  2. Aproperinfo:ppropinfo;
  3. avalue:string;
  4. Begin
  5. Avalue: = GetPropValue (Ademo,' Name ');
  6. Mmo.  Lines.add (' name= ' +avalue);
  7. Avalue: = GetPropValue (Ademo,' age ');
  8. Mmo.  Lines.add (' age= ' +avalue);
  9. Setpropvalue (Ademo,' Name ',' zengzhen ');
  10. Setpropvalue (Ademo,' age ',100);
  11. Avalue: = GetPropValue (Ademo,' Name ');
  12. Mmo.  Lines.add (' name= ' +avalue);
  13. Avalue: = GetPropValue (Ademo,' age ');
  14. Mmo.  Lines.add (' age= ' +avalue);
  15. End

Output:

Delphi Code
    1. Name=peirenlei
    2. age=0
    3. Name=zengzhen
    4. Age=

http://peirenlei.iteye.com/blog/378465

A preliminary examination of Rtti in Delpi

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.