0

I'm writing unit tests for a C# app that contains a private nested class. That class in turn defines a static async method I'd like to test:

public class FooClass {
    private static class BarClass {
        public static async Task<string> BazMethod() { }
    }
}

Now it seems I ought to be able to do the following in my test method:

var barClass = new PrivateType(typeof(FooClass).GetNestedType("BarClass", BindingFlags.NonPublic);
var ret = await (Task<string>)barClass.InvokeStatic("BazMethod");

When I run this, barClass is successfully initialized, and in the debugger I can see its DeclaredMethods include {System.Threading.Tasks.Task'1[System.String] BazMethod()}. But the call to InvokeStatic fails with an MissingMethodException: Method Project.FooClass+BarClass.BazMethod not found.

Perhaps because BazMethod is async, its true name for reflection purposes is decorated, and I need to include that decoration in the name passed to the InvokeStatic call?

  • 3
    No, the method should still exist as normal. There'll be an additional type generated, but I'd expect it to be fine. My guess is that this is to do with being within a private nested type, and the asynchrony has nothing to do with it. That's the first thing you should validate. – Jon Skeet Apr 26 '17 at 06:15
  • possible duplicate: https://stackoverflow.com/questions/14711585/how-to-await-an-async-private-method-invoked-using-reflection-in-winrt?s=1|0.9201 – swe Apr 26 '17 at 06:26
  • 1
    Add `Static` to enum flags in your reflection search – VMAtm Apr 26 '17 at 08:31

0 Answers0