似乎有一个问题,通过通用的参数试图以创建一个参数化的实例时,城堡温莎
There seems to be an issue with passing generic arguments when attempting to create a parametrized instance with Castle Windsor
private static void Main(string[] args)
{
PassGenericParamAtResolutionTime();
Console.ReadLine();
}
private static void PassGenericParamAtResolutionTime()
{
Console.WriteLine("Passing generic argument fails");
var container = new WindsorContainer();
container.Register(Component.For<ISandCoordinator<Simpleton>>()
.ImplementedBy<SandCoordinator<Simpleton>>());
var runtimeConstructorParam = new GenericManager<Simpleton>(
"This Id Does Not Get Through");
var runtimeArguments = new Arguments(
new object[] {runtimeConstructorParam});
var shouldBeParameterizedCoordinator = container
.Resolve<ISandCoordinator<Simpleton>>(runtimeArguments);
Console.WriteLine(shouldBeParameterizedCoordinator.Log);
}
Passing generic argument fails
Birth from parameterless constructor, which should not happen
如果我注释掉参数的构造函数下面,我得到了以下异常:
And if I comment out the parameterless constructor below, I get the following exception:
Castle.MicroKernel.Resolvers.DependencyResolverException was unhandled
Missing dependency.
Component Sand.SandCoordinator`1[[Sand.Simpleton, WindsorSand, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] has a dependency on Sand.IGenericManager`1[Sand.Simpleton], which could not be resolved.
Make sure the dependency is correctly registered in the container as a service, or provided as inline argument.
class SandCoordinator<TSimpleton> : ISandCoordinator<TSimpleton>
where TSimpleton : ISimpleton
{
public SandCoordinator()
{
Log = "Birth from parameterless constructor, which should not happen";
}
public SandCoordinator(IGenericManager<TSimpleton> manager)
{
Log = "Birth from parameterized constructor";
Log += Environment.NewLine + "Born With Manager: " + manager.Id;
}
public string Log { get; private set; }
}
接口ISimpleSandCoordinator:ISandCoordinator&LT;心眼&GT;
和注册非通用接口,那么参数化解析工作,但我不'吨要停止使用泛型类型
如果有这样的申请在温莎城堡?中的错误
interface ISimpleSandCoordinator : ISandCoordinator<Simpleton>
and register the non-generic interface then parametrized resolution works, but I don't want to stop using generic types
Should this be filed as a bug in Castle Windsor?
[使用Castle.Core.dll和Castle.Windsor.dll 3.1.0(2012-08- 05)]
您 SandCoordinator&LT; T&GT;
依赖于 IGenericManager&LT; T&GT;
,不是 GenericManager&LT; T&GT;
当你把一个价值在参数
要温莎你必须要明确它是比它的具体类型别的东西使用。
When you're putting a value in Arguments
that you want Windsor to use as something else than its concrete type you have to be explicit about it.
new Arguments { { typeof(IGenericManager<Simpleton>), runtimeConstructorParam } };