static IAsyncStateMachine GetAsyncMethodThatsAwaitingThisOne(this IAsyncStateMachine sm) { var task = sm.Task(); if (task == null) { return(null); } var continuationDelegates = GetDelegatesFromContinuationObject(task); var continuationDelegate = (continuationDelegates?.Length == 1 ? continuationDelegates[0] as Action : null); if (continuationDelegate == null) { return(null); } var stateMachine = TryGetStateMachineForDebugger(continuationDelegate).Target; if (stateMachine is RunWithCheckpointingAwaiter) { continuationDelegate = (stateMachine as RunWithCheckpointingAwaiter).continuation; stateMachine = TryGetStateMachineForDebugger(continuationDelegate).Target; } var builderField = stateMachine?.GetType().GetField("<>t__builder") ?? stateMachine?.GetType().GetField("$Builder"); if (builderField == null) { return(null); } return(stateMachine as IAsyncStateMachine); }
static string Name(this IAsyncStateMachine sm) { var t = sm.GetType(); var s = t.Name; s = s.Substring(1, s.LastIndexOf(">") - 1); while (true) { t = t.DeclaringType; if (t != null) { s = t.Name + "." + s; } else { return(s + $" [task id {sm.Task()?.Id}]"); } } }