Search code examples
c#reflection.net-assembly.net-5

Passing IConfiguration to constructor using Reflection


I'm trying to use C# Reflection to create an instance, my constructor is receiving two parameters:

  • ILogger
  • IConfiguration

I created the ILogger and passed it to the constructor it's working fine, but the IConfiguration is not working because the object of IConfiguration is returning ConfigurationRoot object

class code:

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");
        }
    }

creating 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);
                    }
                }

Edit: I'm getting the error System.AggregateException: 'One or more errors occurred. (Constructor on type 'WeeklyReport.Weekly' not found.)'


Solution

  • Solution:

    the problem was in ILogger<Weekly> in Weekly class, I was trying to pass ILogger<dynamic> to ILogger<Weekly>

    I just made a change in 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");
            }
        }
    

    and finally, it's working fine.