上面的类中还出现了一个很重要的类型AsyncMethodBuilderCore,简单的了解一下这个类型也很有必要。
namespace System.Runtime.CompilerServices { internal struct AsyncMethodBuilderCore { internal sealed class MoveNextRunner { private readonly ExecutionContext m_context; internal IAsyncStateMachine m_stateMachine; [SecurityCritical] private static ContextCallback s_invokeMoveNext; [SecurityCritical] internal MoveNextRunner(ExecutionContext context, IAsyncStateMachine stateMachine) { this.m_context = context; this.m_stateMachine = stateMachine; } [SecuritySafeCritical] internal void Run() { //这个方法被包装为“继续执行”委托实际执行的代码 //这个方法最终要的作用是给继续执行的代码设置正确的ExecutionContext } [SecurityCritical] private static void InvokeMoveNext(object stateMachine) { ((IAsyncStateMachine)stateMachine).MoveNext(); } } private class ContinuationWrapper { internal readonly Action m_continuation; private readonly Action m_invokeAction; internal readonly Task m_innerTask; internal ContinuationWrapper(Action continuation, Action invokeAction, Task innerTask) { if (innerTask == null) { innerTask = AsyncMethodBuilderCore.TryGetContinuationTask(continuation); } this.m_continuation = continuation; this.m_innerTask = innerTask; this.m_invokeAction = invokeAction; } internal void Invoke() { this.m_invokeAction(); } } internal IAsyncStateMachine m_stateMachine; internal Action m_defaultContextAction; public void SetStateMachine(IAsyncStateMachine stateMachine) { } //上文提到的获取“继续执行”委托的方法 //方法通过包装内部类MoveNextRunner的Run方法来实现 [SecuritySafeCritical] internal Action GetCompletionAction(Task taskForTracing, ref AsyncMethodBuilderCore.MoveNextRunner runnerToInitialize) { Debugger.NotifyOfCrossThreadDependency(); ExecutionContext executionContext = ExecutionContext.FastCapture(); Action action; AsyncMethodBuilderCore.MoveNextRunner moveNextRunner; if (executionContext != null && executionContext.IsPreAllocatedDefault) { action = this.m_defaultContextAction; if (action != null) { return action; } moveNextRunner = new AsyncMethodBuilderCore.MoveNextRunner(executionContext, this.m_stateMachine); action = new Action(moveNextRunner.Run); if (taskForTracing != null) { action = (this.m_defaultContextAction = this.OutputAsyncCausalityEvents(taskForTracing, action)); } else { this.m_defaultContextAction = action; } } else { moveNextRunner = new AsyncMethodBuilderCore.MoveNextRunner(executionContext, this.m_stateMachine); action = new Action(moveNextRunner.Run); if (taskForTracing != null) { action = this.OutputAsyncCausalityEvents(taskForTracing, action); } } if (this.m_stateMachine == null) { runnerToInitialize = moveNextRunner; } return action; } private Action OutputAsyncCausalityEvents(Task innerTask, Action continuation) { } internal void PostBoxInitialization(IAsyncStateMachine stateMachine, AsyncMethodBuilderCore.MoveNextRunner runner, Task builtTask) { //初始化AsyncMethodBuilderCore中的状态机变量。这里发生装箱操作。 } internal static void ThrowAsync(Exception exception, SynchronizationContext targetContext) { //将异常与SynchronizationContext相关联 } internal static Action CreateContinuationWrapper(Action continuation, Action invokeAction, Task innerTask = null) { return new Action(new AsyncMethodBuilderCore.ContinuationWrapper(continuation, invokeAction, innerTask).Invoke); } internal static Action TryGetStateMachineForDebugger(Action action) { //获取用于调试目的的“继续执行”委托 } internal static Task TryGetContinuationTask(Action action) { //获取“继续执行”的Task } } }