这是由How to compare TFunc/TProc containing function/procedure of object?提示的,特别是David对Barry提出的问题的评论。由于我没有博客发布这个问题,我将在这里提出这个问题并回答它。
问题:何时以及如何在Delphi的匿名方法中引用变量?
示例:
procedure ProcedureThatUsesAnonymousMethods;
var V: string;
F1: TFunc<string>;
F2: TFunc<string>;
begin
F1 := function: string
begin
Result := V; // references local variable
end
V := '1';
F2 := function: string
begin
Result := V;
end
V := '2';
ShowMessage(F1);
ShowMessage(F2);
end;
两个ShowMessage
都会显示2
。为什么?如何捕获V
以及何时捕获?
答案 0 :(得分:27)
当你有一个类似问题的函数,你有一个访问局部变量的匿名方法时,Delphi似乎创建了一个TInterfacedObject后代,它捕获所有基于堆栈的变量,因为它是自己的公共变量。使用Barry的技巧来实现TObject和一些RTTI,我们可以看到整个过程。
实现背后的神奇代码可能如下所示:
// Magic object that holds what would normally be Stack variables and implements
// anonymous methods.
type ProcedureThatUsesAnonymousMethods$ActRec = class(TInterfacedObject)
public
V: string;
function AnonMethodImp: string;
end;
// The procedure with all the magic brought to light
procedure ProcedureThatUsesAnonymousMethods;
var MagicInterface: IUnknown;
F1: TFunc<string>;
F2: TFunc<string>;
begin
MagicInterface := ProcedureThatUsesAnonymousMethods$ActRec.Create;
try
F1 := MagicInterface.AnonMethod;
MagicInterface.V := '1';
F2 := MagicInterface.SomeOtherAnonMethod;
MagicInterface.V := '2';
ShowMessage(F1);
ShowMessage(F2);
finally MagicInterface := nil;
end;
end;
当然这段代码不能编译。我很神奇:-)但是这里的想法是在幕后创建一个“魔术”对象,从匿名方法引用的局部变量在魔术对象的公共字段中转换。该对象用作接口(IUnkown),因此它被引用计数。显然,同一个对象捕获所有使用的变量并定义所有匿名方法。
这应该回答“何时”和“如何”。
这是我用来调查的代码。把TButton放在一个空白表格上,这应该是整个单位。当您按下按钮时,您将在屏幕上按顺序看到以下内容:
TForm25.Button1Click$ActRec: TInterfacedObject
:这显示了实现背后的对象,它来自TInterfacedObject OnStack:string
:RTTI会在该对象上发现此字段。Self: TForm25
:RTTI在该对象上发现此字段。它用于获取ClasVar
FRefCount:Integer
- 来自TInterfacedObject Class Var
- ShowMessage的结果。On Stack
- ShowMessage的结果。以下是代码:
unit Unit25;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Rtti;
type
TForm25 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
ClassVar: string;
public
end;
var
Form25: TForm25;
implementation
{$R *.dfm}
procedure TForm25.Button1Click(Sender: TObject);
var F1: TFunc<string>;
F2: TFunc<string>;
OnStack: string;
i: IInterface;
o: TObject;
RC: TRttiContext;
R: TRttiType;
RF: TRttiField;
begin
// This anonymous method references a member field of the TForm class
F1 := function :string
begin
Result := ClassVar;
end;
i := PUnknown(@F1)^;
o := i as TObject;
ShowMessage(IntToStr(Integer(o))); // I'm looking at the pointer to see if it's the same instance as the one for the other Anonymous method
// This anonymous method references a stack variable
F2 := function :string
begin
Result := OnStack;
end;
i := PUnknown(@F2)^;
o := i as TObject;
ShowMessage(IntToStr(Integer(o)));
ShowMessage(o.ClassName + ': ' + o.ClassType.ClassParent.ClassName);
RC.Create;
try
R := RC.GetType(o.ClassType);
for RF in R.GetFields do
ShowMessage(RF.Name + ':' + RF.FieldType.Name);
finally RC.Free;
end;
ClassVar := 'Class Var';
OnStack := 'On Stack';
ShowMessage(F1);
ShowMessage(F2);
end;
end.