未能通过通用的参数与温莎城堡 [英] Failure to pass generic arguments with Castle Windsor
本文介绍了未能通过通用的参数与温莎城堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎有一个问题,通过通用的参数试图以创建一个参数化的实例时,城堡温莎
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;
和注册非通用接口,那么参数化解析工作,但我不'吨要停止使用泛型类型 - 如果有这样的申请在温莎城堡? 中的错误
- I know that if I create a non-generic type
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?
Solutions / Workarounds?
[使用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 } };
这篇关于未能通过通用的参数与温莎城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文