How can I draw an object as an interface, which is a common type of constraint in Delphi

I need to interact with a series of .Net web services. Currently about 150 people. Since delphi 2010 uses Thttprio to achieve this, I am trying to create a common proxy on the client side that can be called to create the corresponding soap service client. Does anyone know how I can pass an httprio object to a generic interface type?

thanks

Below is the proxy function I'm trying to use:

class function Web.Proxy<T>(svc: string): T; var HTTPRIO : THTTPRIO; begin HTTPRIO := THTTPRIO.Create(nil); HTTPRIO.URL := GetServiceURL(svc); Result:= HTTPRIO as T; //<-- Fails with "operator not applicable to this operand type" // Result:= T(HTTPRIO); //<-- also fails, but with "invalid typecast" end; 

The idea is that I can call it the following:

 Web.Proxy<AutmobileServiceSoap>('svc.asmx').GetAutomobile(125); 

The WSDL import has an AutmobileServiceSoap, defined as follows:

 AutmobileServiceSoap = interface(IInvokable) 

And all wsdl imports has a function that returns an httprio object, in a similar way:

 function GetAutomobileServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): AutomobileServiceSoap; const defWSDL = 'http://localhost:8732/Cars.Server/Data/AutomobileService.asmx?WSDL'; defURL = 'http://localhost:8732/Cars.Server/Data/AutomobileService.asmx'; defSvc = 'AutomobileService'; defPrt = 'AutomobileServiceSoap12'; var RIO: THTTPRIO; begin Result := nil; if (Addr = '') then begin if UseWSDL then Addr := defWSDL else Addr := defURL; end; if HTTPRIO = nil then RIO := THTTPRIO.Create(nil) else RIO := HTTPRIO; try Result := (RIO as AutomobileServiceSoap); if UseWSDL then begin RIO.WSDLLocation := Addr; RIO.Service := defSvc; RIO.Port := defPrt; end else RIO.URL := Addr; finally if (Result = nil) and (HTTPRIO = nil) then RIO.Free; end; end; 
+4
source share
2 answers

You must use RTTI to get the GUID of the interface.

 type Web = class class function Proxy<T: IInterface>(svc: string): T; end; class function Web.Proxy<T>(svc: string): T; var HTTPRIO: THTTPRIO; data: PTypeData; begin HTTPRIO := THTTPRIO.Create(nil); HTTPRIO.URL := GetServiceURL(svc); data := GetTypeData(TypeInfo(T)); if ifHasGuid in data.IntfFlags then begin HTTPRIO.QueryInterface(data.Guid, Result); end; end; 

If you specify an IInterface constraint, you can be sure that T is always an interface (otherwise you should also check TypeKind TypeInfo).

+5
source

Since T can be anything, you must clearly tell Delphi that T will be an interface. Since all interfaces inherit from IUnknown , you can write this:

  Web = class class function Proxy<T :IUnknown>(svc : string) : T; end; 
+2
source

Source: https://habr.com/ru/post/1489277/


All Articles