构造函数中有一个具有两个相同类型参数的类。
我使用autofac注入依赖项,但无法找到注入相同类型的2个参数的方法。注射时有什么方法可以说明吗?
class test
{
test(Iconnection con1, Iconnection con2)
{ }
} 我想通过autofac注入con1和con2,但无法做到。
发布于 2015-03-26 01:53:28
在注册您的ResolvedParameter类时,可以使用Test构造:
builder.RegisterType<Test>()
.As<ITest>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(IConnection) && pi.Name == "con1",
(pi, ctx) => [code resolving con1 from ctx]))
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(IConnection) && pi.Name == "con2",
(pi, ctx) => [code resolving con2 from ctx]));或者,如果您正在处理相同类型的配置不同的实例,我建议将构造函数签名更改为接受接受参数以区分连接的工厂类型(例如IConnectionFactory)。例如:
interface IConnectionFactory
{
IConnection CreateConnection(string name);
}https://stackoverflow.com/questions/29269543
复制相似问题