Here is my test code: the extension method GetInstructions
is from here: https://gist.github.com/jbevain/104001
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
typeof(TestClass)
.GetMethods()
.Where(method => method.Name == "Say" || method.Name == "Hello")
.ToList()
.ForEach(method =>
{
var calls = method.GetInstructions()
.Select(x => x.Operand as MethodInfo)
.Where(x => x != null)
.ToList();
Console.WriteLine(method);
calls.ForEach(call =>
{
Console.WriteLine($"\t{call}");
call.GetGenericArguments().ToList().ForEach(arg => Console.WriteLine($"\t\t{arg.FullName}"));
});
});
Console.ReadLine();
}
}
class TestClass
{
public async Task Say()
{
await HelloWorld.Say<IFoo>();
HelloWorld.Hello<IBar>();
}
public void Hello()
{
HelloWorld.Say<IFoo>().RunSynchronously();
HelloWorld.Hello<IBar>();
}
}
class HelloWorld
{
public static async Task Say<T>() where T : IBase
{
await Task.Run(() => Console.WriteLine($"Hello from {typeof(T)}.")).ConfigureAwait(false);
}
public static void Hello<T>() where T : IBase
{
Console.WriteLine($"Hello from {typeof(T)}.");
}
}
interface IBase
{
Task Hello();
}
interface IFoo : IBase
{
}
interface IBar : IBase
{
}
}
Here is run result as the screenshot shown:
System.Threading.Tasks.Task Say()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder Create()
Void Start[<Say>d__0](<Say>d__0 ByRef)
ConsoleApp1.TestClass+<Say>d__0
System.Threading.Tasks.Task get_Task()
Void Hello()
System.Threading.Tasks.Task Say[IFoo]()
ConsoleApp1.IFoo
Void RunSynchronously()
Void Hello[IBar]()
ConsoleApp1.IBar
NON-ASYNC calls got correct generic parameters, but ASYNC calls cannot.
My question is: where are the generic parameters stored for ASYNC calls?
Thanks a lot.
internal string ConstructName
from referencesource.microsoft.com/#mscorlib/system/… , which blocked me to find out interface name. – Murtha