如何创建Visual Studio字符串可视化器? [英] How to create a Visual-Studio string visualizer?

查看:114
本文介绍了如何创建Visual Studio字符串可视化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为IDictionary或ICollection创建可视化器

I was trying to create a visualizer for IDictionary or ICollection

然后像简单的可视化工具(没有对话框;我的意思是当将鼠标悬停在变量上时出现的常用字符串可视化工具,请参见下图),我要创建自定义文本,我想将集合转换为类型列表(IE StringCollection到List(Of String)或List),然后我将能够在可视化器中看到它. 或对于字典"显示以列出键和值的可视化器.

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.

这是我想到的:

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;
        }
    }
}

这些是用于数组和多维数组的VS可视化器:

These are VS visualizers for array and multidimentional arrays:

我想对ICollection(或IList),IDictionary等使用类似的东西.

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

请注意,在数组中,可视化工具显示每个嵌套的objcet. 这实际上是我想要实现的目标:

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

.

尝试可视化列表,您将看到有一个私有值_items,因此您可以查看其项目. 我想在收藏和字典中实现类似的目的.

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.

推荐答案

在Code Project上有很多示例.这是我经验最丰富的一个: DataSet Visualizer

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

我自己安装并使用过它,所以我知道它可以工作. Is确实比您需要的要先进,因为它实际上显示了整个ADO数据集,但是代码应该很容易修改.

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:

项目1

项目2

这篇关于如何创建Visual Studio字符串可视化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆