这是我正在尝试构建的更通用的客户端函数,它将允许我调用返回 TDBXReader 的不同服务器端过程。现在它有效,但我面临几个问题,我需要你的帮助:
- (最重要的)你怎么看这种方法?有什么建议/建议吗?
- 如何释放 vLClient(实际上是 TSrvMethodClient)?
- 为什么我不允许将第二个参数传递给 Create 方法?
谢谢你。
function askServerTo_give(SQLConn: TSQLConnection; procName: String; cds: TClientDataSet): Boolean;
var
ctx : TRttiContext;
SrvRTTI: TRttiType;
vLClient, vLReader: TValue;
//LClient : TSrvMethodsClient;
begin
Result := False;
vLClient := nil;
vLReader := nil;
ctx := TRttiContext.Create;
SrvRTTI := ctx.GetType(TSrvMethodsClient.ClassInfo);
vLClient := SrvRTTI.GetMethod('Create').Invoke(SrvRTTI.AsInstance.MetaclassType, [ SQLConn.DBXConnection ] );
//vLClient := SrvRTTI.GetMethod('Create').Invoke(SrvRTTI.AsInstance.MetaclassType, [ SQLConn.DBXConnection , False] ); // Error!
//LClient := TSrvMethodsClient.Create( SQLConn.DBXConnection, False);
try
vLReader := SrvRTTI.GetMethod( procName ).Invoke(vLClient, []);
if (vLReader.AsObject as TDBXReader) <> nil then begin
TDBXDataSetReader.CopyReaderToClientDataSet((vLReader.AsObject as TDBXReader), cds);
Result := not cds.IsEmpty;
end;
finally
(vLReader.AsObject as TDBXReader).Free; //FreeAndNil() doesn`t work
//(vLClient.AsObject as TSrvMethodsClient).Free; // Error!
ctx.Free;
end;
end;