"This function requires all threads to evaluate" 在 VS 自定义可视化工具中的“new AdhocWorkspace()”之后

"This function requires all threads to evaluate" after `new AdhocWorkspace()` within VS custom visualizer

我正在尝试编写一个 maps expressions in an expression tree to Roslyn syntax nodes 的可视化工具,以便为表达式树生成代码。语法树生成的一部分是对 AdhocWorkspace 构造函数的调用。

当我 运行 使用 VisualizerDevelopmentHost 的可视化工具时,一切正常:

using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Linq.Expressions;

namespace _testVisualizer {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            Expression<Func<bool>> expr = () => true;
            var data = new TestVisualizerData(expr);
            var visualizerHost = new VisualizerDevelopmentHost(data, typeof(TestVisualizer));
            visualizerHost.ShowVisualizer();

            Console.ReadKey(true);
        }
    }
}

但是当我尝试通过 Visual Studio UI 使用可视化工具时(将鼠标悬停在 expr 上,单击放大镜图标并选择我的可视化工具),我得到了以下消息:

Unable to perform function evaluation on the process being debugged.

Additional information

The function evaluation requires all threads to run.

我发现以下是触发错误的原因:

workspace = new AdhocWorkspace();

分配给 Mapper class (source) 上的 workspace 字段。

为什么调用 AdhocWorkspace 构造函数会触发此警告?我该如何解决这个问题?


这是一个演示问题的 MCVE:

using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;

[assembly: DebuggerVisualizer(typeof(_testVisualizer.TestVisualizer), typeof(_testVisualizer.TestVisualizerDataObjectSource), Target = typeof(System.Linq.Expressions.Expression), Description = "Test Visualizer")]

namespace _testVisualizer {
    public class TestVisualizer : DialogDebuggerVisualizer {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) {
            var data = (TestVisualizerData)objectProvider.GetObject();
            var txt = new TextBlock();
            txt.SetBinding(TextBlock.TextProperty, "Status");
            var window = new Window {
                DataContext = data,
                Content = txt
            };
            window.ShowDialog();
        }
    }

    [Serializable]
    public class TestVisualizerData {
        public TestVisualizerData() { }
        public TestVisualizerData(System.Linq.Expressions.Expression expr) {
            var workspace = new AdhocWorkspace();
            Status = "Success";
        }
        public string Status { get; set; }
    }

    public class TestVisualizerDataObjectSource : VisualizerObjectSource {
        public override void GetData(object target, Stream outgoingData) {
            var expr = (System.Linq.Expressions.Expression)target;
            var data = new TestVisualizerData(expr);
            base.GetData(data, outgoingData);
        }
    }
}

也许可以利用 AdhocWorkspace alternate constructor 在单个线程中使用 SyntaxNode API。

我已经提交了 issue