jeudi 16 mai 2019

Acrivator.CreateInstance of a class that takes an Interface through constructor, but i only have access to the ConstructorInfo

My goal is to create a service that locates all Classes that implement a certain Interface and returns an instance of them.

The Issue is that I need to generate the parameters for the constructors depending on the signature of the specific constructors.

However when i try to create an instance of a Class that requires an Interface as a parameter for the constructor I have problems.

            var locatedTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
                .Where(x => typeof(IScheduledAction).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract); // get all classes that implement the interface

 foreach (Type type in locatedTypes)
            {
                var t = (IScheduledAction)Activator.CreateInstance(type); // at this point i get an Error

                if(CheckDayOfWeek(t.DaysOfWeek) && DateTime.Now.Hour == (int)t.HoursOfDay.GetValue(0))
                                 t.RunNow();

            }

The error i Get is that the Class i found does not have a constructor.

Please help.





Aucun commentaire:

Enregistrer un commentaire