mardi 25 avril 2017

How to await a static async method in a private type?

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?





Aucun commentaire:

Enregistrer un commentaire