#1 2019-06-14 13:19:53

squirrel
Member
Registered: 2015-08-13
Posts: 155

Accessing interface instance without generics

Currently on Windows, I can access an instance of an interface like this

TMyInterface(aRestServer.Service<IStaff>).NewSetting := 10;

Since

function Service<T: IInterface>: T; 

is only declared for Delphi2010 and up, how would I do this on Linux using fpc?

Would this be the same:?

var tmp: TMyInterface;
...
if aRestServer.Services.Info(TypeInfo(IStaff)).Get(tmp) then
  TMyInterface(tmp).NewSetting := 10;

Last edited by squirrel (2019-06-14 13:32:21)

Offline

#2 2019-06-14 13:44:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Accessing interface instance without generics

The "generics" stuff is just another way of putting it. But in fact, it is not the best option.

The best is to register the interface, then write:

initialization
  TInterfaceFactory.RegisterInterfaces([ IStaff ]);
end.

var tmp: IStaff;
if aRestServer.Services.Resolve(IStaff,tmp) then
  tmp.NewSetting := 10;

Warning: you are making confusion between interfaces (IStaff) and classes (TMyInterface). So your code won't work.

Offline

Board footer

Powered by FluxBB