Getting variable name and type info in VS2019 custom visualizer using IVisualizerObjectProvider or ENVDTE

Abhijit Damle 1 Reputation point
2021-09-17T03:03:16.173+00:00

I am trying to implement a custom visualizer for some of our own classes as suggested at https://learn.microsoft.com/en-us/visualstudio/debugger/walkthrough-writing-a-visualizer-in-csharp?view=vs-2019

Write a visualizer in C# - Visual Studio (Windows) | Microsoft Learn
Choose File > New > Project.In the language drop-down, choose C#.In the search box, type class library, and then choose Class Library (.NET Framework).Click Next.In the dialog box that appears, type the name MyFirstVisualizer, and then click Create.. For the visualizer project, make sure you select a .NET Framework class library and not .NET.
learn.microsoft.com
. In this visualizer we want to only customize displayed content of the object value according to variable type instead of the instance value. For example: for statement IInterface1 i1 = new Class1(); (definition of them are pasted below), we will set the target of the visualizer to be Class1, when opening visualizer for variable i1 in watch window, we want to know the value retrieved from object provider is of type IInterface1, so we only display property Value1, and don’t display Value2.

public interface IInterface1

{

           int Value1 { get; set; }  

}

public class Class1 : IInterface1

{

           public int Value1 { get; set; }  

           public int Value2 { get; set; }  

}

Currently the problem is that object provider of visualizer only provide value of the instance of Class1, it doesn’t contain information of the variable’s type (IInterface1 in this example), we can manage to get EnvDTE.DTE in this visualizer, but we still need the variable name to call Debugger.GetExpression method to get the type information, which is also missing in current visualizer.

So my question is: is there any way for us to retrieve information of type IInterface1 for the Class1 instance retrieved from object provider? Like finding out information of the row in watch window on whose value the visualizer was opened, maybe using EnvDTE.DTE or something else, then get the variable name or type from the row; Or is there any other way?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,484 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
958 questions
0 comments No comments
{count} votes