Click here to Skip to main content
12,242,449 members (49,113 online)
Click here to Skip to main content
Add your own
alternative version

Stats

10K views
23 bookmarked
Posted

Visual Studio Visualizer: Part 3 - Collection visualizer

, 10 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This project creates a Visual Studio visualizer for .NET collections

Introduction 

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

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:

[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:

[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:

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:

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:

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:

The export to excel file preserves the tree hierarchy:

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)

Share

About the Author

Frederico Regateiro
Software Developer
Portugal Portugal
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionVisualizing list of string Pin
Wilsade29-Mar-16 3:29
memberWilsade29-Mar-16 3:29 
GeneralMy vote of 5 Pin
wsc091810-Jun-13 16:23
memberwsc091810-Jun-13 16: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 10 Jun 2013
Article Copyright 2013 by Frederico Regateiro
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid