C# 如何使用autofac DI通过参数构造函数解析动态创建实例
我的代码是这样的C# 如何使用autofac DI通过参数构造函数解析动态创建实例,c#,asp.net-mvc,dependency-injection,autofac,dynamic-programming,C#,Asp.net Mvc,Dependency Injection,Autofac,Dynamic Programming,我的代码是这样的 private void installData() { var dataInstallServices = new List<IDataInstallationService>(); var dataInstallServiceTypes=_typeFinder.FindClassesOfType<IDataInstallationService>(); foreach (var dataInstallServiceType
private void installData()
{
var dataInstallServices = new List<IDataInstallationService>();
var dataInstallServiceTypes=_typeFinder.FindClassesOfType<IDataInstallationService>();
foreach (var dataInstallServiceType in dataInstallServiceTypes)
dataInstallServices.Add((IDataInstallationService)Activator.CreateInstance(dataInstallServiceType));
foreach (var installServie in dataInstallServices)
installServie.InstallData();
}
我注册了所有依赖项,但没有为此对象定义无参数构造函数。使用
Activator.CreateInstance(类型t)时出现异常
method您应该确保该类型有无参数constructor可用于您要传递给其参数的类型,否则它将抛出您得到的异常
为什么默认constructor不存在?
在类中指定带参数的构造函数时,编译器将删除默认构造函数
using System;
public class Program
{
public static void Main()
{
Test t = new Test() //will give you compile time error.
Test t1 = new Test(""); //should work fine.
}
}
public class Test
{
public Test(string a)
{
}
}
使用另一个重载方法传递构造函数参数,如下所示:
Activator.CreateInstance(typeof(T), param1, param2);
该方法的MSDN文档是。如果您使用的是AutoFac,则不需要使用Activator来创建实例 假设您在一个名为
DataService
的类中尝试执行上述操作,该类具有以下依赖项:
public class DataInstallerA : IDataInstaller {
public DataInstallerA(SubDependencyA a){}
}
public class DataInstallerB : IDataInstaller {
public DataInstallerA(SubDependencyB b){}
}
通过以下AutoFac注册:
builder.RegisterType<SubDependencyA>();
builder.RegisterType<SubDependencyB>();
builder.RegisterType<DataInstallerA>().As<IDataInstaller>();
builder.RegisterType<DataInstallerA>().As<IDataInstaller>();
builder.RegisterType<DataService>();
builder.RegisterType();
RegisterType();
builder.RegisterType().As();
builder.RegisterType().As();
RegisterType();
然后,您的数据服务可能看起来像:
public class DataService
{
private IEnumerable<IDataInstaller> _dataInstallers;
public DataService(IEnumerable<IDataInstaller> dataInstallers) {
_dataInstallers = dataInstallers;
}
public void Install() {
foreach (var installer in _dataInstallers)
installer.InstallData();
}
}
公共类数据服务
{
私有IEnumerable\u数据安装程序;
公共数据服务(IEnumerable DataInstaller){
_数据安装程序=数据安装程序;
}
public-void-Install(){
foreach(数据安装程序中的var安装程序)
installer.InstallData();
}
}
DataService
不需要知道如何创建所有的idatanstaller
实例,AutoFac可以做到这一点,它只需要一组实例
请注意,即使您实际上没有注册
IEnumerable
AutoFac,但在注册类型时会隐式提供一些额外的注册。请参阅。IDataInstallationService
类型可能没有任何of参数。我的问题是如何动态解析它?@MuhdAlavu允许您将n个参数传递给CreateInstance
方法。@MuhdAlavu查看此处了解有关msdn文档的更多信息。此答案没有解决此问题。您为什么要使用反射来解决此问题实例化您的服务,而不是Autofac?根据我的解释,您已经向Autofac注册了所有依赖项。
public class DataService
{
private IEnumerable<IDataInstaller> _dataInstallers;
public DataService(IEnumerable<IDataInstaller> dataInstallers) {
_dataInstallers = dataInstallers;
}
public void Install() {
foreach (var installer in _dataInstallers)
installer.InstallData();
}
}