TMethod.Data指向哪?(100分)

  • 主题发起人 Another_eYes
  • 开始时间
A

Another_eYes

Unregistered / Unconfirmed
GUEST, unregistred user!
TMethod.code是不是指向例程的入口?(过程或函数的地址)
那么data呢? 指向DS? SS?还是具体offset?
 
正如你所说,TMethod.Code是指向过程或函数的指针.
data是什么,我也不知道.看了Delphi下的一个源程序,学着做了一个例子,
但是,Data到底有什么意义也不很明确.

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure RunProc(ProcName:pChar);
published
procedure Hello;
procedure GoodBye;
end;

var
Form1: TForm1;
type
TMyProcedure=procedure of object;

implementation

{$R *.DFM}

procedure TForm1.Hello;
begin
ShowMessage('Hello');
end;

procedure TForm1.GoodBye;
begin
ShowMessage('GoodBye');
end;

procedure TForm1.RunProc(ProcName:pChar);
var
theProc:TMyProcedure;
begin
try
//MethodAddress取得Published方法的地址
TMethod(theProc).Code:=Self.MethodAddress(ProcName);
if TMethod(theProc).Code = nil then Exit;
TMethod(theProc).Data := Self; //这句话加与不加没有区别?
theProc;
except
Exit;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunProc('Hello');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
RunProc('GoodBye');
end;

end.
 
如果是一个Component的方法,
TMethod.date就指向这个Component自身.
 
既然Tmethod 的定义
type
TMethod = record
Code, Data: Pointer;
都应该可以做指向函数或过程的指针,原来做应用时,是可以用
data 的指针指向Object 的
 
多人接受答案了。
 
有幸参观此站点,非常高兴能结识大家.

实际上TMethod.Data是对象实例(Instance)的首地址,
TMethod的结构定义方式就是目前O-O语言实现的原理.
Code和Data分离,Code表示类的首地址,Data表示类的
实例的首地址.方法表示对Code的偏移量,字段(Field)
表示对Data的偏移量.

这种方法可以用来挂上一些保护的事件.
 

Similar threads

回复
0
查看
732
不得闲
S
回复
0
查看
760
SUNSTONE的Delphi笔记
S
S
回复
0
查看
974
SUNSTONE的Delphi笔记
S
S
回复
0
查看
639
SUNSTONE的Delphi笔记
S
S
回复
0
查看
795
SUNSTONE的Delphi笔记
S
顶部