Search code examples
linqvisual-studio-2010debuggervisualizer

Debugger Visualizer [Visual Studio 2010] - System.Linq.Expressions.Expression - not showing magnifying glass


I have been trying to build a debugger visualizer for a linq Expression.

I know one exists already, but I would like to create my own and add additional functionality.

I made this quick prototype. The magnifying glass will not show up; however, if I change the one line of code to "Target = typeof(System.String)", the magnifying glass shows up.

Any help would be appreciated.

using System.IO;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;

[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(VisualizerPrototype.MyDebuggerVisualizer),
    typeof(VisualizerPrototype.MyDebuggerVisualizerObjectSource),
    Target = typeof(System.Linq.Expressions.Expression),
    Description = "My Debugger Visualizer")]
namespace VisualizerPrototype
{
    public class MyDebuggerVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            var text = string.Empty;
            using (var sr = new StreamReader(objectProvider.GetData()))
            {
                text = sr.ReadToEnd();
            }

            MessageBox.Show(text);
        }
    }

    public class MyDebuggerVisualizerObjectSource : VisualizerObjectSource
    {
        public override void GetData(object target, System.IO.Stream outgoingData)
        {
            var sw = new StreamWriter(outgoingData);
            sw.WriteLine("YO");
            sw.Flush();
        }
    }
}

Solution

  • For anybody reading this in the future, I discovered the source of my problem. The target type for a debugger visualizer must be the runtime type and not an inherited type.

    Target = typeof(System.Linq.Expressions.ConstantExpression)
    Expression expr = Expression.Constant(1); //visualizer shows up
    
    Target = typeof(System.Linq.Expressions.Expression)
    Expression expr = Expression.Constant(1); //visualizer doesn't show up