• rtti获取类的字段和属性和方法


    通过 Rtti 单元的 TRttiContext(是个 record), 可以方便地获取类的方法、属性、字段的列表.
    --------------------------------------------------------------------------------


    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;

    //获取 TButton 类的方法
    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;

    //获取 TButton 类的属性
    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;

    //获取 TButton 类的字段
    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;

    //获取获取 TButton 类的方法集合、属性集合、字段集合
    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 类共有 %d 个方法', [t.Name, Length(ms)]));
      Memo1.Lines.Add(Format('%s 类共有 %d 个属性', [t.Name, Length(ps)]));
      Memo1.Lines.Add(Format('%s 类共有 %d 个字段', [t.Name, Length(fs)]));
    end;

    end.

     
  • 相关阅读:
    蛙蛙推荐:用.net 4.0构建新浪微博开发平台应用
    蛙蛙推荐:Backbone官方文档翻译
    蛙蛙推荐:javascript异步任务流
    蛙蛙推荐:sqlserver三种分页方式性能比较
    蛙蛙推荐:css背景图片固定居中,div阴影,背景半透明,div圆角,DIV渐变
    如何高效的利用新浪微博获取信息
    蛙蛙推荐:gevent并发发送UDP包演示
    蛙蛙推荐:神马ORM都是浮云,看看史上最简单最好用的OleDbHelper
    蛙蛙推荐:写了一个简单的延迟队列
    PostgreSQL中with和without time zone两者有什么区别 规格严格
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940570.html
Copyright © 2020-2023  润新知