Max
Max

Reputation: 869

Unable to find private void generic function with GetMethod

I have a function with the following signature:

private void Foo<TPoco>(IContext context, List<TPoco> pocos, DateTime modifiedDateTime)
    where TPoco : MyAbstractClass

And I cannot find this function in GetMethods().

Based on this SO post ( GetMethod for generic method ), I have tried these binding flags:

GetType().GetMethods(BindingFlags.Public 
    | BindingFlags.NonPublic 
    | BindingFlags.Instance 
    | BindingFlags.Static 
    | BindingFlags.FlattenHierarchy
)

And this finds 14 other methods in the class, but not this one. It does find this method:

protected void Bar<TContext, TPoco>(List<TPoco> pocosToPersist, TContext context)
    where TContext : IContext, new()
    where TPoco : MyAbstractClass

So the only difference is the access level - but then I can see other private functions.

Even if I change the binding flags to something simpler (which, from what I understand, shouldn't make new items visible):

GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance )

I still don't see the function.

Upvotes: 2

Views: 229

Answers (2)

From Orbonia
From Orbonia

Reputation: 676

I wasn't having an issue with the return void type, but having the same symptoms with GetMethods() - i.e. it would not return the method. I found GetRuntimeMethods() instead, and that included the generic method I was looking for.

In terms of wider understanding and other ways to approach this - this thread (I think) is more relevant: How do I use reflection to call a generic method?

Upvotes: 0

Max
Max

Reputation: 869

As the comments on the post point out, the code should have worked. The issue is that the class I am defining the function in is abstract, so I wasn't able to find the private function.

If I do this.GetType().BaseType.GetMethods( BindingFlags.NonPublic | BindingFlags.Instance), the function shows up as expected.

Upvotes: 1

Related Questions