[英]DELPHI: Declare HTTPRIO.OnBeforeExecute in TComponent
使用Delphi 2005,我創建了一個測試應用程序(使用TForm)來測試SOAP API。 不幸的是,該API聲明了Delphi保留的一些枚舉(應用程序,系統和終端)。 我在SOAP文件(_Application,_Terminal和_System)中重命名了枚舉,並且能夠編寫OnBeforeExecute和OnAfterExecute方法,以便在提交前后用原始名稱替換這些重命名的枚舉。
我現在正嘗試將其合並到我的大型項目中,並希望在類文件(無格式)中捕獲此SOAP API的所有代碼。 使用我的測試器應用程序,我在工具窗體中從窗體中添加了一個THTTPRIO對象,並且可以在對象檢查器中輕松設置OnBeforeExecute和OnAfterExecute方法。 現在使用類(一個TComponent),我無法像使用表單一樣使用Tool Palette添加THTTPRIO對象。 我試圖通過代碼創建THTTPRIO對象,但出現一些錯誤。
我收到錯誤E2009 incompatible types: 'Parameter lists differ'
FEPS_HTTPRIO.OnAfterExecute := HTTPRIOAfterExecute;
(請參見下面的代碼)
為什么我得到這個錯誤,但是卻沒有得到FEPS_HTTPRIO.OnBeforeExecute := HTTPRIOBeforeExecute;
的錯誤FEPS_HTTPRIO.OnBeforeExecute := HTTPRIOBeforeExecute;
以及如何在類中實現這兩種方法?
這是我通過代碼創建THTTPRIO的方法:
unit c_MoSh;
interface
uses classes, forms, Windows, SysUtils, c_MoShAPI, InvokeRegistry, controls;
Type
TMoSh = class(TComponent)
private
...
procedure HTTPRIOBeforeExecute(const MethodName: string;
var SOAPRequest: WideString);
procedure HTTPRIOAfterExecute(const MethodName: string;
var SOAPResponse: TStream);
...
constructor TMoSh.Create();
begin
FEPS_HTTPRIO := THTTPRIO.Create(self);
FEPS_HTTPRIO.OnBeforeExecute := HTTPRIOBeforeExecute;
FEPS_HTTPRIO.OnAfterExecute := HTTPRIOAfterExecute; <-- Error line
end;
procedure TMosquitoShield.HTTPRIOBeforeExecute(const MethodName: string;
var SOAPRequest: WideString);
var
tmpString: TStringList;
begin
try
SOAPRequest := StringReplace(SOAPRequest,'<ReversalType>_Application','<ReversalType>Application',[RfReplaceAll]);
SOAPRequest := StringReplace(SOAPRequest,'<ReversalType>_System','<ReversalType>System',[RfReplaceAll]);
SOAPRequest := StringReplace(SOAPRequest,'<CardholderPresentCode>NotPresent2','<CardholderPresentCode>NotPresent',[RfReplaceAll]);
SOAPRequest := StringReplace(SOAPRequest,'<DeviceInputCode>NotUsed3','<DeviceInputCode>NotUsed',[RfReplaceAll]);
except
on ER : ERemotableException do
ShowMessage(ER.ClassName + ' error raised, with message : ' + ER.FaultDetail + ' :: '
+ ER.Message);
on E : Exception do
ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
end;
end;
procedure TMosquitoShield.HTTPRIOAfterExecute(const MethodName: string;
var SOAPResponse: TStream);
var
tmpString: TStringList;
begin
try
tmpString := TStringList.Create;
SOAPResponse.Position := 0;
tmpString.LoadFromStream(SOAPResponse);
tmpString.Text := StringReplace(tmpString.Text,'Application','_Application',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'System','_System',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'<PASSUpdaterOption>Null','<PASSUpdaterOption>Null2',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'<TransactionSetupMethod>Null','<TransactionSetupMethod>Null3',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'<Device>Null','<Device>Null4',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'<ConsentCode>NotUsed','<ConsentCode>NotUsed2',[RfReplaceAll]);
tmpString.Text := StringReplace(tmpString.Text,'<DeviceInputCode>NotUsed','<DeviceInputCode>NotUsed3',[RfReplaceAll]);
SOAPResponse.Position := 0;
tmpString.SaveToStream(SOAPResponse);
except
on ER : ERemotableException do
ShowMessage(ER.ClassName + ' error raised, with message : ' + ER.FaultDetail + ' :: '
+ ER.Message);
on E : Exception do
ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
end;
end;
您的方法簽名必須與事件類型的簽名完全匹配。 在HTTPRIOAfterExecute
方法中的SOAPResponse
參數之前刪除var
。
至於您描述的名稱沖突,可以通過在代碼元素(枚舉成員,變量,類型等) SOAP_API.Application
加上單元名稱來避免它們,單元名稱為: SOAP_API.Application
用於SOAP枚舉,以及Forms.Application
for Delphi Application
全局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.