mardi 10 juillet 2018

Reflection order by property

I have a contract as following:

public interface IConsumer<in T>
{
    void Handle(T eventMessage);
    int Order { get; }
}

Then I have consumers that Implement this contact.

Finally, I would like to find and create objects that implements this interface.

But I want to sort it by order, but the following dont work.

        var iconsumer = typeof(IConsumer<T>);
        var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes())
                             .Where(p => iconsumer.IsAssignableFrom(p) && p.IsClass);

        var consumers = new List<IConsumer<T>>();

        foreach (var type in types.OrderBy(r => r.GetProperty("Order")))
        { 
            consumers.Add((IConsumer<T>) Activator.CreateInstance(type));
        } 

How can I sort consumers by Order?

Thanks.





Aucun commentaire:

Enregistrer un commentaire