|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
namespace VisualizerLib
{
public class DictionaryProcessor
{
#region Member variables/Properties
private IDictionary dict;
private IList m_keyList;
public IList KeyList
{
get { return m_keyList; }
set { m_keyList = value; }
}
#endregion
#region Constructor
public DictionaryProcessor(IDictionary d)
{
dict = d;
}
#endregion
#region Methods
public void CreateKeyList()
{
// Generate the key list
m_keyList = new ArrayList();
foreach (object key in dict.Keys)
{
m_keyList.Add(key);
}
}
public void GetDictionary(bool getKeys, object key, DataGridView grid)
{
// Check if it is not empty
if (dict.Count > 0)
{
object singleObj = null;
IDictionary<string, string> columns = new Dictionary<string, string>();
if (getKeys)
{
// Retrieve the key structure
foreach (object keyItem in dict.Keys)
{
singleObj = keyItem;
break;
}
}
else
{
// Retrieve the value structure
foreach (object valueItem in dict.Values)
{
singleObj = valueItem;
break;
}
}
Type typeObj = singleObj.GetType();
if (typeObj.IsPrimitive == false && typeObj.Name != "String")
{
// Retrieve the properties
PropertyInfo[] properties = typeObj.GetProperties();
foreach (PropertyInfo property in properties)
{
grid.Columns.Add(property.Name, property.Name);
grid.Columns[grid.Columns.Count - 1].SortMode = DataGridViewColumnSortMode.NotSortable;
columns.Add(property.Name, "P");
}
// Retrieve the fields
FieldInfo[] fields = typeObj.GetFields();
foreach (FieldInfo field in fields)
{
if (field.Name != "Empty")
{
grid.Columns.Add(field.Name, field.Name);
grid.Columns[grid.Columns.Count - 1].SortMode = DataGridViewColumnSortMode.NotSortable;
columns.Add(field.Name, "F");
}
}
}
// Value type
if (columns.Count == 0)
{
grid.Columns.Add(typeObj.Name, typeObj.Name);
grid.Columns[grid.Columns.Count-1].SortMode = DataGridViewColumnSortMode.NotSortable;
columns.Add(typeObj.Name, "V");
}
// Retrieve the fields and properties values
foreach (DictionaryEntry dicEntry in dict)
{
object item = null;
if (getKeys)
{
item = dicEntry.Key;
}
else
{
item = dict[key];
}
object[] row = new object[columns.Count];
int countRow = 0;
foreach (KeyValuePair<string, string> column in columns)
{
string name = column.Key;
string type = column.Value;
if (type == "P")
{
// Retrieve property value
row[countRow] = item.GetType().GetProperty(name).GetValue(item, null);
}
if (type == "F")
{
// Retrieve field value
row[countRow] = item.GetType().GetField(name).GetValue(item);
}
if (type == "V")
{
// Retrieve value type
row[countRow] = item.ToString();
}
countRow++;
}
grid.Rows.Add(row);
if (getKeys == false)
{
break;
}
}
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started development 37 years from now, since MSX basic. Started Windows programming with VB 2.0 and Web programming with ASP 3.0. Then I built Windows Forms, Web Applications, NT services and WPF applications using Microsoft.NET. I am MCP in Visual Basic 6.0, MCAD and MCSD.NET in Framework 1.1, MCPD Web in Framework 2.0, MCTS in .NET 3.5 workflow, MCTS in .NET 3.5 communication foundation, windows presentation foundation and MVC applications. Built MVC Web Application and WCF services using Micro Services architecture proposed by me. Working with AI projects to improve the business performance and customer experience. Besides programming I love running, swimming, reading and movies.