Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#

Visual Studio Visualizer: Part 3 - Collection visualizer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
10 Jun 2013CPOL2 min read 19.5K   23   4
This project creates a Visual Studio visualizer for .NET collections

Introduction 

This project creates a Visual Studio visualizer for .NET list collections: List<T>.

Image 1

Project website at: http://vsdatawatchers.codeplex.com.

Background

Part 1 is available here: http://www.codeproject.com/Articles/578777/Visual-Studio-Visualizer-Part-1

If your not familiar with visual studio visualizers, I recommend reading part 1.

Part 2 is available here: http://www.codeproject.com/Articles/584739/Visual-Studio-Visualizer-Part-2-Entity-Framework.

Using the code

Reflection on the List<>

To read the values from the list to the visualizer, a simple DTO is used:

C#
[Serializable]
public class Entity
{
    public Guid Pk = Guid.NewGuid();

    public string Name { get; set; }

    List<EntityProperties> properties;
    public List<EntityProperties> Properties
    {
        get
        {
            if (properties == null)
                properties = new List<EntityProperties>();
            return properties;
        }
        set { properties = value; }
    }

    List<Entity> nestedEntities;
    public List<Entity> NestedEntities
    {
        get
        {
            if (nestedEntities == null)
                nestedEntities = new List<Entity>();
            return nestedEntities;
        }
        set { nestedEntities = value; }
    }
}

This class consist of a name, a list of properties and a list of other complex properties called NestedEntities.

The EntityProperties is a container for simple .NET properties like strings, ints and others:

C#
[Serializable]
public class EntityProperties
{
    public string PropertyName { get; set; }
    public object Value { get; set; }
    public string PropertyType { get; set; }
}

The reflection method iterates in all the list enties and fills a List<Entity> that is then passed to the visualizer:

C#
private void WriteObject(object o, Entity currentEntity)
{
    if (o is IEnumerable)
    {
        foreach (object element in (IEnumerable)o)
        {
            if (element is IEnumerable && !(element is string))
            {
                if (level < depth)
                {
                    level++;
                    WriteObject(element, currentEntity);
                    level--;
                }
            }
            else
                WriteObject(element, currentEntity);
        }
    }
    else
    {
        //reads the object members
        MemberInfo[] members = o.GetType().GetMembers(
                       BindingFlags.Public | BindingFlags.Instance);

        Entity newEntity = new Entity();
        newEntity.Name = o.ToString();

        //we are only interested in properties and fields
        foreach (MemberInfo m in members.Where(p => p is FieldInfo || p is PropertyInfo))
        {
            FieldInfo f = m as FieldInfo;
            PropertyInfo p = m as PropertyInfo;
            if (f != null || p != null)
            {
                Type t = f != null ? f.FieldType : p.PropertyType;

                //reads the properties values, name value and type
                EntityProperties property = new EntityProperties();
                property.PropertyName = m.Name;
                property.PropertyType = t.FullName;

                if (t.IsValueType || t == typeof(string))
                {
                    try
                    {
                        //reads the value
                        property.Value = f != null ? f.GetValue(o) : p.GetValue(o, null);
                        newEntity.Properties.Add(property);
                    }
                    catch { }
                }
            }
        }

        //add to new entity or as a nested entity
        if (currentEntity == null)
            entities.Add(newEntity);
        else
            currentEntity.NestedEntities.Add(newEntity);

        //where we read the complex properties until the defined depth
        if (level < depth)
        {
            foreach (MemberInfo m in members.Where(p => p is FieldInfo || p is PropertyInfo))
            {
                FieldInfo f = m as FieldInfo;
                PropertyInfo p = m as PropertyInfo;
                if (f != null || p != null)
                {
                    Type t = f != null ? f.FieldType : p.PropertyType;
                    if (!(t.IsValueType || t == typeof(string)))
                    {
                        try
                        {
                            object value = f != null ? f.GetValue(o) : p.GetValue(o, null);
                            if (value != null)
                            {
                                level++;
                                WriteObject(value, newEntity);
                                level--;
                            }
                        }
                        catch { }
                    }
                }
            }
        }
    }
}

For every entry in the list, that is visualized, it's used recursion, as for every complex related entity.

This version only supports visualization on List<> but in mind of future versions a extension method was created:

C#
public static List<Entity> DumpMe(this Object o, int depth = 2)
{
    string filename = System.IO.Path.Combine(Environment.GetFolderPath(
      Environment.SpecialFolder.ApplicationData), 
      "CodeVisualizer", "CodeVisualizerOptions.xml");

    if (File.Exists(filename))
    {
        XDocument doc = XDocument.Load(filename);
        XElement xe = doc.Descendants("Depth").FirstOrDefault();
        if (xe != null)
            int.TryParse(xe.Value, out depth);
    }

    return Dump.ReadObject(o, depth).entities;
}

The Dump.ReadObject simply call the method described. Here it's used XDocument to read the depth, this value can be persisted by the user on the visualizer. Store this value in the properties project doesn't work the value returned is always 0, and reading the file with XmlSerializer also return 0, so the XDocument is a work around.

Use the visualizer

In Part 1 of this series is all the explanation to how to use the visualizer.

The visualizer displays the information in a treeview, with all the complex properties as children nodes. The information presented is the name of the property, it's value and Type.

There is the possibility to filter the information, essential in lists with a large amount of items. The filter can be done by Name, Value or Type:

Image 2

There is a possibility to change the depth, that is, the complex properties (children) that are displayed. This limit exists for performance reasons, the tests were made using entity framework, and not using a depth limit, the visualizer could retrieve all the database, one property at a time!!!  To change the depth used, change the value in the depth textbox and restart the visualizer.

For more detailed operations there is a possibility to export the data to XML and Excel:

Image 3

The export to excel file preserves the tree hierarchy:

Image 4

This first version, v20130601 doesn't allow to change the values, it will be implemented in the next version.

History

  • 2013-06-03: Article creation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralInstalling in VS2015 Pin
Data Quarry21-May-17 19:51
professionalData Quarry21-May-17 19:51 
QuestionVisualizing list of string Pin
Wilsade29-Mar-16 2:29
Wilsade29-Mar-16 2:29 
QuestionRe: Visualizing list of string Pin
max212121-Jul-16 3:17
max212121-Jul-16 3:17 
GeneralMy vote of 5 Pin
wsc091810-Jun-13 15:23
wsc091810-Jun-13 15:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.