mardi 6 octobre 2015

Reflecting over inherited private methods [duplicate]

This question already has an answer here:

I wrote this function:

public static MethodInfo[] GetMethods<T>()
{
    return typeof(T).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
}

It seems to work fine for classes that do not inherit any other type:

class A
{
    private void Foo() { }
}

var methods = GetMethods<A>(); // Contains void Foo()

But when I run the function on a class that inherits another, it fails to get the base class's private methods:

class B : A
{
    private void Bar() { }
}

var methods = GetMethods<B>(); // Contains void Bar(), but not void Foo() :(

I know that I could define void Foo() as protected, but I am dealing with third party code, where I am not able to do so.

So how do I iterate over the private functions of a class and it's parent classes?





Aucun commentaire:

Enregistrer un commentaire