How to create a Visual-Studio string visualizer?

谁都会走 提交于 2019-12-11 12:31:51

问题


I was trying to create a visualizer for IDictionary or ICollection

Then like the simple visualizer (without dialog; I mean the ususal string visualizer that appears when hovering the variable, see image below), I want to make my custom text, I want to cast the collection to its type's list (I.E. StringCollection to List(Of String) or List) and then I will be able to see it in the visualizer. Or for Dictionaries show to lists visualizers for keys and for values.

Any ideas how to implement or even how to start?

I will update my question soon.

This is something I thought about:

using System.Collections.Specialized;
using System.Collections;

namespace ConsoleApplication2
{
    static class Program
    {
        static void Main(string[] args)
        {
            System.Collections.Specialized.StringCollection collection = new StringCollection();
            collection.AddRange(new string[] { "string1", "string2", "sting3" });
            string[] visualizable = collection.ConvertToVisualizableList();

            Dictionary<string,string> dic = new Dictionary<string,string>
            {
              {"key1","value"},
              {"key2","value"}
            };
            string[,]      visualizable2 = dic.ConvertToVisualizableDictionary();



        }

        static string[] ConvertToVisualizableList(this IList collection)
        {
            lock (collection)
            {
                if (collection == null) return null;
                int length = collection.Count;
                string[] list = new string[length];

                for (int i = 0; i < length; i++)
                {
                    object item = collection[i];
                    if (item != null) list[i] = item.ToString();
                }

                return list.ToArray();
            }
        }

        static string[,] ConvertToVisualizableDictionary(this IDictionary dictionary)
        {
            if (dictionary == null) return null;
            int length = dictionary.Count;
            string[,] list = new string[length, 2];

            int i = 0;
            foreach (object item in dictionary.Keys)
            {
                list[i, 0] = item.ToString();
                object value = dictionary[item];
                if(value!=null) list[i, 1] = value.ToString();
                i++;
            }
            return list;
        }
    }
}

These are VS visualizers for array and multidimentional arrays:

I want to use something similar for ICollection (or IList), IDictionary etc.

Note that in arrays, the visualizer shows every nested objcet. This is actually what I want to achieve:

.

Try to visualize a List and you will see that there is a private value _items, so you can see its items. I want to achieve something similar in collection and dictionary.


回答1:


There are a number of examples on Code Project. This is the one i have the most experience with: DataSet Visualizer

i have installed and used it myself so i know it works. Is is more advanced than you need since it actually displays entire ADO data sets but the code should be pretty easy to modify.

Here are a couple of other links to check out as well:

Project 1

Project 2




回答2:


I've found somthing that already exists:

http://www.codeproject.com/KB/macros/ListVisualizer.aspx, but it will still not show objects.



来源:https://stackoverflow.com/questions/1294230/how-to-create-a-visual-studio-string-visualizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!