为了扩展Felice给出的答案,我认为根据接受的答案发布我得出的解决方案会很有用。
目前我的命令是通过一个映射的,IDictionary<TKey,TValue>
但很快就会移动到另一个介质(XML、JSON 等)。
这是我为用户输入命令注册组件的方式:
public void InstallUserCommands(IWindsorContainer container)
{
var commandToClassMappings = new Dictionary<string, string>
{
{"move", "MoveCommand"},
{"locate","LocateSelfCommand"},
{"lookaround","LookAroundCommand"},
{"bag","LookInBagCommand"}
};
foreach (var command in commandToClassMappings)
{
var commandType = Type.GetType("TheGrid.Commands.UserInputCommands." + command.Value);
container.Register(Component.For(commandType).Named(command.Key));
}
}
并解决实例:
public UserCommandInputMapperResponse Invoke(UserCommandInputMapperRequest request)
{
var container = new WindsorContainer();
container.Install(FromAssembly.This());
IUserInputCommand instance;
try
{
instance = container.Resolve<IUserInputCommand>(request.CommandName.ToLower().Trim());
}
catch (Exception)
{
instance = null;
}
return new UserCommandInputMapperResponse
{
CommandInstance = instance
};
}