プログラムそのものには意味はありません。
プレビュー会に説明があったようにprivateなメソッドは読めませんでした。
メソッドの可視属性を示す型があったので読めてもよいと思ったのですが・・・
また、GetDeclaredMethodsをつかうと自分が宣言したメソッドのみ
リストできます。
以下ソースです。
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, ExtCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- LabeledEdit1: TLabeledEdit;
- Button2: TButton;
- ListBox1: TListBox;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private 宣言 }
- //procedure SakaTest2;
- protected
- //procedure SakaTest1;
- procedure SakaTest2;
- public
- { Public 宣言 }
- procedure SakaTest2;
- procedure SakaTest1;
- end;
- var
- Form1: TForm1;
- implementation
- uses
- Rtti,TypInfo;
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- ctx : TRttiContext;
- rtm : Rtti.TRttiMethod;
- obj : TObject;
- Args : Array of TValue;
- begin
- //
- ctx := TRttiContext.Create;
- //rtm := ctx.GetType(obj).GetMethod(LabeledEdit1.Text);
- rtm := ctx.GetType(Self.ClassType).GetMethod(LabeledEdit1.Text);
- if rtm <> nil then
- begin
- //obj := Self;
- rtm.Invoke(Self,Args);
- end;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- var
- ctx : TRttiContext;
- rtmary : TArray<rtti.trttimethod>;
- rtm : TRttiMethod;
- obj : TObject;
- Args : Array of TValue;
- rtp : TRttiType;
- begin
- ctx := TRttiContext.Create;
- //rtm := ctx.GetType(obj).GetMethod(LabeledEdit1.Text);
- //ctx.FindType()
- //rtp := ctx.GetType(Self.ClassType);
- //rtp.IsPublicType := false;
- rtmary := ctx.GetType(Self.ClassType).GetDeclaredMethods;
- ListBox1.Clear;
- if rtmary <> nil then
- begin
- for rtm in rtmary do
- begin
- ListBox1.Items.Add(rtm.Name);
- if rtm.Name = LabeledEdit1.Text then
- begin
- rtm.Invoke(Self,Args);
- end;
- end;
- end;
- end;
- procedure TForm1.SakaTest1;
- begin
- Application.MessageBox('Hello Delphi','First Text');
- end;
- procedure TForm1.SakaTest2;
- begin
- Application.MessageBox('Hello Delphi','Second Text');
- end;
- end.
- </rtti.trttimethod>
引数つきのメソッドコールはまた次回
0 件のコメント:
コメントを投稿