dimanche 19 septembre 2021

How do I use reflection to call a generic method that accepts lambda input

What I'm trying to do is similar to the question bellow:

How do I use reflection to call a generic method?

but the function I'm trying to call needs a lambda input

(I'm adding masstransit consumers from domain assemblies by selecting assemblies where they are inheriting different interfaces)

The code:

        private static void AddLimitedConcurrencyMessageRecievers(IServiceCollectionBusConfigurator x, Assembly[] assemblies)
        {
            IEnumerable<Type> types = assemblies.SelectMany(s => s.GetExportedTypes())
                .Where(w => w.IsClass && !w.IsAbstract && w.IsPublic && typeof(IMessageReciever).IsAssignableFrom(w)).ToList();
            
            foreach (var item in types)
            {
                //x.AddConsumer<item>(y => y.UseConcurrentMessageLimit(1)); this is how I wished it would be like
                MethodInfo method = typeof(IRegistrationConfigurator).GetMethod(nameof(IRegistrationConfigurator.AddConsumer));
                MethodInfo generic = method.MakeGenericMethod(item);
                generic.Invoke(null, y => y.UseConcurrentMessageLimit(1)); //this still does not work
            }
                
        }




Aucun commentaire:

Enregistrer un commentaire