How to get an instance of a child interface from ServiceLocator in Spring4d?

I am very new to the Spring4D platform and ask for help.

I have the following classes and interfaces:

ICommand = interface

TCommand = class(TInterfacedObject, ICommand)

IVecadCommand = interface(ICommand)

TVecadCommand = class(TCommand, IVecadCommand)

TVecadCommandJPG = class(TVecadCommand, IVecadCommand)

TCommandDeckelJPG = class(TVecadCommandJPG, IVecadCommand)

then register the component:

GlobalContainer.RegisterComponent<TCommandDeckelJPG>.Implements<IVecadCommand>('deckel_jpg');

then I am trying to create an object using ServiceLocator:

var
  i: Integer;
  com: ICommand;
begin
  Result := nil;
  com := ServiceLocator.GetService<ICommand>(actionName);
  com.setSession(designSession);
  Result := com;
end;

As a result of execution, I have an exception:

Invalid class typecast

To avoid an exception, I do this:

var
  i: Integer;
  com: IVecadCommand;
begin
  Result := nil;
  com := ServiceLocator.GetService<IVecadCommand>(actionName);
  com.setSession(designSession);
  Result := com;
end;

then everything is all right.

Point: I have to use its TContainer in this case as a repository for TCommand and inherited classes. So I have to use ServiceLocator first.

What to do to avoid an exception and use ICommand, but not IVecadCommand in TContainer?

Thank. I will be happy to provide you with additional information.

+4
source share
1

IVecadCommand, ICommand.

, , , , (, - ).

, , . .

ICommand, .Implements<ICommand> . , , , , , IVecadCommand. IVecadCommand ( , ICommand), IVecadCommand ICommand.

: - , . IVecadCommand ICommand, TValue.AsType<T>. . . . . , , , , .

10.04.2014: , , .

+6

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


All Articles