dimanche 11 novembre 2018

List a class's generic superclasses?

I have an inheritance hierarchy:

public interface IValidator<TItem>

public abstract class Validator<TItem, TId> : IValidator<IItem>

public class MyValidator : Validator<MyItem, int>

I want to find every interface and superclass of MyValidator. I can do the interfaces easily with

return validator.GetType().GetInterfaces();

But when I try the same for superclasses, all I get back is System.Object, not Validator<MyItem, int>.

var type = validator.GetType();
return AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(supertype => type.IsSubclassOf(supertype));

How can I get the full inheritance hierarchy?





Aucun commentaire:

Enregistrer un commentaire