Delphi 中使用“with”创建的引用对象实例

2024-01-30

有没有办法引用使用“with”语句创建的对象实例?

Example:

with TAnObject.Create do
begin
  DoSomething(instance);
end;

其中 DoSomething 将使用实例引用,就像将实例从声明的变量引用传递给创建的对象一样。

Example:

AnObject := TAnObject.Create;

Thanks.


好吧,你可以使用这样的方法:

// implement:

type
  TSimpleMethod = procedure of object;

function GetThis(const pr: TSimpleMethod): TObject;
begin
  Result := TMethod(pr).Data;
end;

// usage:

  with TStringList.Create do
  try
    CommaText := '1,2,3,4,5,6,7,8,9,0';
    ShowText(TStringList(GetThis(Free)));
  finally
    Free;
  end;

或类助手:

type 
  TObjectHelper = class helper For TObject
  private
    function GetThis: TObject; Inline;
  public
    property This: TObject read GetThis;
  end;

...

function TObjectHelper.GetThis: TObject;
begin
  Result := Self;
end;

但实际上,以前的回复是正确的:你最好忘记“with”语句。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Delphi 中使用“with”创建的引用对象实例 的相关文章

随机推荐