【发布时间】:2014-03-31 13:12:22
【问题描述】:
我是Spring4D框架的新手,请求帮助。
我有下一个类和接口:
ICommand = interface
TCommand = class(TInterfacedObject, ICommand)
IVecadCommand = interface(ICommand)
TVecadCommand = class(TCommand, IVecadCommand)
TVecadCommandJPG = class(TVecadCommand, IVecadCommand)
TCommandDeckelJPG = class(TVecadCommandJPG, IVecadCommand)
然后我注册一个组件:
GlobalContainer.RegisterComponent<TCommandDeckelJPG>.Implements<IVecadCommand>('deckel_jpg');
然后我尝试在 ServiceLocator 的帮助下创建一个对象:
var
i: Integer;
com: ICommand;
begin
Result := nil;
com := ServiceLocator.GetService<ICommand>(actionName);
com.setSession(designSession);
Result := com;
end;
作为执行的结果我有一个异常:
Invalid class typecast
为了避免我这样做的例外:
var
i: Integer;
com: IVecadCommand;
begin
Result := nil;
com := ServiceLocator.GetService<IVecadCommand>(actionName);
com.setSession(designSession);
Result := com;
end;
然后一切正常。
重点是在这种情况下我必须使用它 TContainer 作为 TCommand 和继承类的存储库。所以我必须首先使用 ServiceLocator。
我应该怎么做才能避免异常并在TContainer中使用ICommand而不是IVecadCommand?
谢谢。将很乐意提供更多详细信息。
【问题讨论】: