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
{
MemberInfo[] members = o.GetType().GetMembers(
BindingFlags.Public | BindingFlags.Instance);
Entity newEntity = new Entity();
newEntity.Name = o.ToString();
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;
EntityProperties property = new EntityProperties();
property.PropertyName = m.Name;
property.PropertyType = t.FullName;
if (t.IsValueType || t == typeof(string))
{
try
{
property.Value = f != null ? f.GetValue(o) : p.GetValue(o, null);
newEntity.Properties.Add(property);
}
catch { }
}
}
}
if (currentEntity == null)
entities.Add(newEntity);
else
currentEntity.NestedEntities.Add(newEntity);
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.