要在菲菲給出的答案擴大我覺得這一定是有用的張貼我來到了基於公認的答案的解決方案。
目前我的命令均通過一個IDictionary<TKey,TValue>
映射但很快就會移動到另一個介質(XML,JSON等)。
這裏是我如何註冊compnents用戶輸入命令:
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
};
}
你爲什麼不解決,而不是首先使用Activator.CreateInstance與溫莎對象的地方之一? – 2011-02-07 04:50:20
我現在在做。請參閱下面的答案以瞭解我當前的實際情況。 – 2011-02-07 04:51:12