2

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.

Community
  • 1
  • 1
Max
  • 849
  • 9
  • 24
  • 1
    Your last attempt should work perfectly. On what object are you calling `GetType` ? – Zein Makki Jul 28 '16 at 12:24
  • 1
    The problem must lie somewhere else. Copy the function and reflection calls into a new project or something like .Net Fiddle and you will see that the code you posted here works flawless. – thehennyy Jul 28 '16 at 12:27
  • Yup, you are both right - the private function is in an abstract class, so GetType() was getting a different type than I expected. – Max Jul 28 '16 at 12:30

2 Answers2

1

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.

Max
  • 849
  • 9
  • 24
0

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?

From Orbonia
  • 616
  • 5
  • 16