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
};
}