为异步调用保存的通用参数在哪里?在哪里可以找到它的名称或其他信息?

Where are the generic parameters saved for Async calls? Where to find its name or other information?

这是我的测试代码:扩展方法GetInstructions来自这里: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
    {

    }
}

这里是 运行 结果如屏幕截图所示:

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

非异步调用获得正确的通用参数,但异步调用不能。

我的问题是:ASYNC 调用的通用参数存储在哪里?

非常感谢。

您可以尝试获取泛型方法信息,这样您就可以从中找到 IFoo 泛型类型参数(代码取自 msdn):

private static void DisplayGenericMethodInfo(MethodInfo mi)
    {
        Console.WriteLine("\r\n{0}", mi);

        Console.WriteLine("\tIs this a generic method definition? {0}", 
            mi.IsGenericMethodDefinition);

        Console.WriteLine("\tIs it a generic method? {0}", 
            mi.IsGenericMethod);

        Console.WriteLine("\tDoes it have unassigned generic parameters? {0}", 
            mi.ContainsGenericParameters);

        // If this is a generic method, display its type arguments.
        //
        if (mi.IsGenericMethod)
        {
            Type[] typeArguments = mi.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // IsGenericParameter is true only for generic type
                // parameters.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}  parameter position {1}" +
                        "\n\t\t   declaring method: {2}",
                        tParam,
                        tParam.GenericParameterPosition,
                        tParam.DeclaringMethod);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }

async 方法没那么简单。

C# 编译器将从 async 方法中生成综合状态机。所以 TestClass.Say 方法的主体将被编译器完全覆盖。如果您想更深入地了解异步状态机,可以阅读 this great blog post

回到你的问题。

编译器会将方法体替换为如下内容:

<Say>d__0 stateMachine = new <Say>d__0();
stateMachine.<>4__this = this;
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
这段代码中的

<Say>d__0 是编译器生成的类型。它的名称中包含特殊字符,以防止您在代码中使用此类型。

<Say>d__0 是一个 IAsyncStateMachine 实现。主要逻辑包含在它的MoveNext方法中。

它看起来类似于:

TaskAwaiter awaiter;
if (state != 0)
{
    awaiter = HelloWorld.Say<IFoo>().GetAwaiter();
    if (!awaiter.IsCompleted)
    {
        // ...
        builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
        return;
    }
}
else
{
    awaiter = this.awaiter;
    state = -1;
}

awaiter.GetResult();
HelloWorld.Hello<IBar>();

请注意,您的 HelloWorld.Say<IFoo>() 调用现在在这里,在这个方法中,而不是在您原来的 TestClass.Say 中。

因此,要从您的方法中获取通用类型信息,您需要检查 MoveNext 状态机方法而不是原始的 TestClass.Say。在那里搜索调用说明。

像这样:

Type asyncStateMachine = 
    typeof(TestClass)
    .GetNestedTypes(BindingFlags.NonPublic)
    .FirstOrDefault(
        t => t.GetCustomAttribute<CompilerGeneratedAttribute>() != null 
        && typeof(IAsyncStateMachine).IsAssignableFrom(t));

MethodInfo method = asyncStateMachine.GetMethod(
    nameof(IAsyncStateMachine.MoveNext),
    BindingFlags.NonPublic | BindingFlags.Instance);

List<MethodInfo> calls = method.GetInstructions()
    .Select(x => x.Operand as MethodInfo)
    .Where(x => x != null)
    .ToList();

// etc

输出:

Void MoveNext()
        System.Threading.Tasks.Task Say[IFoo]()
                ConsoleApp1.IFoo
        System.Runtime.CompilerServices.TaskAwaiter GetAwaiter()
        Boolean get_IsCompleted()
        Void AwaitUnsafeOnCompleted[TaskAwaiter,<Say>d__0](System.Runtime.CompilerServices.TaskAwaiter ByRef, <Say>d__0 ByRef)
                System.Runtime.CompilerServices.TaskAwaiter
                ConsoleApp1.TestClass+<Say>d__0
        Void GetResult()
        Void Hello[IBar]()
                ConsoleApp1.IBar
        Void SetException(System.Exception)
        Void SetResult()

请注意,此代码取决于当前 IAsyncStatMachine 实现内部。如果 C# 编译器更改了该内部实现,则此代码可能会中断。