CSDN博客

img wangyunyong0905

利用反射机制动态调用方法

发表于2008/10/3 20:11:00  491人阅读

分类: DELPHI

 unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ObjAuto, StdCtrls;

type
{$MethodInfo ON}
TTestCall = class(TPersistent)
public
    function Add(X,Y: integer): integer;
    procedure Msg(s: string);
end;
{$MethodInfo OFF}

TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
tc:TTestCall;
mih: PMethodInfoHeader;
res:integer;
begin
tc := TTestCall.Create;
mih := Objauto.GetMethodInfo(tc,'Msg');
ObjAuto.ObjectInvoke(tc, mih, [1], ['orz...']);
tc.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
tc:TTestCall;
mih: PMethodInfoHeader;
res:integer;
begin
tc := TTestCall.Create;
mih := Objauto.GetMethodInfo(tc,'Add');
res:=ObjAuto.ObjectInvoke(tc, mih, [1,2], [10,20]);
ShowMessage(Inttostr(res));
tc.Free;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
end;

{ TTestCall }

function TTestCall.Add(X, Y: integer): integer;
begin
result := X+Y;
end;

procedure TTestCall.Msg(s: string);
begin
ShowMessage(s);
end;

end.
0 0

相关博文

我的热门文章

img
取 消
img