Пример #1
0
        protected override string ToStringCore()
        {
            var result = TaskKind switch
            {
                //TaskKind.FromTaskCompletionSource => Contract.AssertNotNull(_taskCompletionSource).ToString(),
                TaskKind.ContinuationTaskFromTask => $"{base.ToStringCore()}{Environment.NewLine}ContinueWith on: {ContinueWithNameToString()}",
                //TaskKind.AsyncMethodTask => Contract.AssertNotNull(_asyncStateMachine).ToString(),
                // var result = $"{InsAndOuts()} [{DisplayStatus.ToString()}] {ClrInstance?.ToString(Types) ?? ""}";
                //TaskKind.WhenAll => $"{InsAndOuts()} [{DisplayStatus.ToString()}] {ClrInstance.ToString(Types)}",
                TaskKind.TaskRun => $"{InsAndOuts()} Task.Run ({ClrInstance.ObjectAddress})",
                _ => base.ToStringCore(),
            };

            return(result);

            string ContinueWithNameToString()
            {
                // A current task node is the task created by calling ContinueWith method.
                // So we can grab m_action field and get a "method name" that was given to ContinueWith method.
                // Is it possible to get a method name by the method pointer?
                var action = ClrInstance["m_action"].Instance;

                // Here is a comment in Task.cs
                // internal object m_action;
                // The body of the task.  Might be Action<object>, Action<TState> or Action.  Or possibly a Func.
                // If m_action is set to null it will indicate that we operate in the
                // "externally triggered completion" mode, which is exclusively meant
                // for the signalling Task<TResult> (aka. promise). In this mode,
                // we don't call InnerInvoke() in response to a Wait(), but simply wait on
                // the completion event which will be set when the Future class calls Finish().
                // But the event would now be signalled if Cancel() is called
                if (action.IsNull)
                {
                    return("externally triggered completion mode");
                }

                var methodPtr = action["_methodPtr"].Instance;

                var runtime    = Context.Runtime;
                var methodInfo = runtime.GetMethodByAddress((ulong)(long)methodPtr.Value);

                if (methodInfo == null)
                {
                    methodPtr  = action["_methodPtrAux"].Instance;
                    methodInfo = runtime.GetMethodByAddress((ulong)(long)methodPtr.Value);
                }

                Contract.AssertNotNull(methodInfo, "Can't find method name for a delegate");
                var signature = methodInfo.GetFullSignature();

                return(TypeNameHelper.TrySimplifyMethodSignature(signature));
            }
        }