C# 派生类如何注入?;
Net核心Web API 父类没有空构造函数吗 派生类Autofac注入 如果在参数之后添加了injection类,则不能使用它C# 派生类如何注入?;,c#,asp.net,.net,C#,Asp.net,.net,Net核心Web API 父类没有空构造函数吗 派生类Autofac注入 如果在参数之后添加了injection类,则不能使用它 public class A { public A(string e1,string e2){} } public class B:A { private readonly IProductService _productService; public B(IP
public class A
{
public A(string e1,string e2){}
}
public class B:A
{
private readonly IProductService _productService;
public B(IProductService productService):base(string e1,string e2)
{
_productService = productService
}
public void test()
{
_productService.AddProduct("");
}
}
AutoFac在配置时没有问题
_出现productService异常您应该这样尝试:
public B(IProductService productService, string e1,string e2):base(e1,e2)
{
_productService = productService
}
然后像这样为此类注册配置Autofac:
builder.Register(c => new B(c.Resolve<IProductService>(), "e1_val","e2_val"));
请记住,Autofac具有很大的灵活性,请查看他们的文档:了解更多信息。嗨,Joey,你能完成你的问题并告诉我们发生了什么异常吗?!您必须将某些内容传递到基构造函数中<代码>公共B(IPProductService产品服务):基(字符串e1,字符串e2)不正确。这些代码都不会编译
A
不能有两个仅因其参数名称不同而不同的构造函数。如何将IPProductService注入此派生类?
builder.RegisterType<B>().As<IB>()
.WithParameter("e1", "e1value")
.WithParameter("e2", "e2value");