vendredi 22 mars 2019

Get all static fields from a class

I know that there are many questions like this, but I couldn't find any answer to what I'm trying to do.

Considering the following abstract class:

public abstract class TestBase
{
    public static ITest Test => Container.Resolve<ITest>();
    public static ITest1 Test1 => Container.Resolve<ITest1>();
    public static ITest2 Test2 => Container.Resolve<ITest2>();
    public static ITest3 Test3 => Container.Resolve<ITest3>();
}

I'm trying to get all the fields that inherit from an interface IDummy like this:

    var members = typeof(TestBase).GetMembers(BindingFlags.Static | BindingFlags.Public)
        .Where(f => f.GetType().IsAssignableFrom(typeof(IDummy)) == true);

but the list is empty. Without adding the where clause ".Where(f => f.GetType().IsAssignableFrom(typeof(IDummy)) == true)" I get all the results including the getters for the fields.

Probably is something trivial, but as I'm not so familiar with reflection I can't figure out what I'm doing wrong.





Aucun commentaire:

Enregistrer un commentaire