You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在无Sender参数的THTTPRIO事件中访问HTTPRIO.Tag属性?

解决THTTPRIO.OnBeforeExecute中访问Tag属性的方法

由于THTTPRIO的OnBeforeExecute事件未提供Sender参数,无法直接通过Sender获取组件实例,可通过以下几种方式解决:

1. 直接引用组件实例(适用于固定的单个/少数实例)

如果你的THTTPRIO是设计时在窗体/DataModule上创建的固定实例,直接在事件处理方法中引用组件变量即可访问Tag属性:

procedure TForm1.HTTPRIO1BeforeExecute(const MethodName: string; SOAPRequest: TStream);
begin
  if HTTPRIO1.Tag = 99 then
  begin
    // 在此编写你的业务逻辑
  end;
end;

2. 匿名方法捕获实例(适用于动态创建的实例)

如果是动态创建多个THTTPRIO实例,可在绑定事件时使用匿名方法捕获当前实例,直接访问其Tag属性:

var
  NewRIO: THTTPRIO;
begin
  NewRIO := THTTPRIO.Create(nil);
  try
    NewRIO.Tag := 99; // 预先赋值Tag
    // 绑定事件时用匿名方法捕获NewRIO实例
    NewRIO.OnBeforeExecute := procedure(const MethodName: string; SOAPRequest: TStream)
      begin
        if NewRIO.Tag = 99 then
        begin
          // 执行对应逻辑
        end;
      end;
    // 后续使用NewRIO调用SOAP接口...
  finally
    // 注意:若实例需长期存在,不要在此释放;否则需确保匿名方法不会引用已释放的实例
    // NewRIO.Free;
  end;
end;

注意:使用匿名方法时需关注实例的生命周期,避免引用已被释放的对象导致内存错误。

3. 自定义继承类(适用于需要统一处理大量实例的场景)

如果需要在多个实例中统一处理该事件,且希望事件能提供Sender参数,可以继承THTTPRIO并扩展事件定义:

type
  TMyHTTPRIO = class(THTTPRIO)
  private
    FOnBeforeExecuteEx: procedure(Sender: TObject; const MethodName: string; SOAPRequest: TStream) of object;
    procedure DoBeforeExecuteEx(const MethodName: string; SOAPRequest: TStream);
  protected
    procedure DoBeforeExecute(const MethodName: string; SOAPRequest: TStream); override;
  public
    property OnBeforeExecuteEx: procedure(Sender: TObject; const MethodName: string; SOAPRequest: TStream) of object 
      read FOnBeforeExecuteEx write FOnBeforeExecuteEx;
  end;

procedure TMyHTTPRIO.DoBeforeExecute(const MethodName: string; SOAPRequest: TStream);
begin
  inherited;
  DoBeforeExecuteEx(MethodName, SOAPRequest);
end;

procedure TMyHTTPRIO.DoBeforeExecuteEx(const MethodName: string; SOAPRequest: TStream);
begin
  if Assigned(FOnBeforeExecuteEx) then
    FOnBeforeExecuteEx(Self, MethodName, SOAPRequest); // 将Self作为Sender传入
end;

之后使用TMyHTTPRIO替代原THTTPRIO,在OnBeforeExecuteEx事件中即可通过Sender获取实例并访问Tag属性:

procedure TForm1.MyHTTPRIO1BeforeExecuteEx(Sender: TObject; const MethodName: string; SOAPRequest: TStream);
begin
  if TMyHTTPRIO(Sender).Tag = 99 then
  begin
    // 处理逻辑
  end;
end;

内容的提问来源于stack exchange,提问作者Carlos Matos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 22:50:38