使用反射将 IConfiguration 传递给构造函数

Passing IConfiguration to constructor using Reflection

我正在尝试使用 C# Reflection 创建实例,我的构造函数接收两个参数:

我创建了 ILogger 并将其传递给构造函数,它工作正常,但是 IConfiguration 不工作,因为 IConfiguration 的对象正在返回 ConfigurationRoot 对象

class代码:

public class Weekly 
    {
        public Guid SessionId { get; set; }
        private readonly ILogger<Weekly> _logger;
        private readonly IConfiguration _configuration;
        public Weekly(ILogger<Weekly> ilogger, IConfiguration configuration)
        {
            _logger = ilogger;
            _configuration = configuration;
            SessionId = Guid.NewGuid();
            Console.WriteLine("Weekly inestance created with id: " + SessionId.ToString());
            _logger.LogInformation("Weekly inestance created with id: " + SessionId.ToString());
        }
        public void Report()
        {
            Console.WriteLine("Weekly report called");
            _logger.LogInformation("Weekly report called");
        }
    }

正在创建 object :

    foreach (ConstructorInfo constructor in constructors)
                {
                    ParameterInfo[] parameters = constructor.GetParameters();
                    if (parameters.Count() > 0)
                    {
                        object[] objs = new object[parameters.Count()];
                        for (int i = 0; i < parameters.Count(); i++)
                        {
                            if (typeof(ILogger).IsAssignableFrom(parameters[i].ParameterType))
                            {
                                objs[i] = _serviceProvider.GetService(typeof(ILogger<dynamic>)) as ILogger<dynamic>;
                            }
                            else if (typeof(IConfiguration).IsAssignableFrom(parameters[i].ParameterType))
                            {
                         //here i'm adding IConfiguration
                                objs[i] = _serviceProvider.GetService<IConfiguration>();
                            }
                        }
                        instance = Activator.CreateInstance(type, objs);
                    }
                    else
                    {
                        instance = Activator.CreateInstance(type);
                    }
                }

编辑: 我收到错误 System.AggregateException: '发生一个或多个错误。 (未找到 'WeeklyReport.Weekly' 类型的构造函数。)'

解法:

问题出在 Weekly class 中的 ILogger<Weekly>,我试图将 ILogger<dynamic> 传递给 ILogger<Weekly>

我刚刚在每周 class 中进行了更改:

public class Weekly 
    {
        public Guid SessionId { get; set; }
        private readonly ILogger<dynamic> _logger;
        private readonly IConfiguration _configuration;
        public Weekly(ILogger<dynamic> ilogger, IConfiguration configuration)
        {
            _logger = ilogger;
            _configuration = configuration;
            SessionId = Guid.NewGuid();
            Console.WriteLine("Weekly inestance created with id: " + SessionId.ToString());
            _logger.LogInformation("Weekly inestance created with id: " + SessionId.ToString());
        }
        public void Report()
        {
            Console.WriteLine("Weekly report called");
            _logger.LogInformation("Weekly report called");
        }
    }

最后,一切正常。