|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
namespace VisualizerLib
{
public class ListProcessor
{
#region Constructor
private IList list;
public ListProcessor(IList l)
{
list = l;
}
#endregion
#region Methods
public void GetList(DataGridView dgvList)
{
// Check if it is not empty
if (list.Count > 0)
{
object singleObj = list[0];
IDictionary<string, string> columns = new Dictionary<string, string>();
Type typeObj = singleObj.GetType();
if (typeObj.IsPrimitive == false && typeObj.Name != "String")
{
// Retrieve the properties
PropertyInfo[] properties = singleObj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
dgvList.Columns.Add(property.Name, property.Name);
columns.Add(property.Name, "P");
}
// Retrieve the fields
FieldInfo[] fields = singleObj.GetType().GetFields();
foreach (FieldInfo field in fields)
{
dgvList.Columns.Add(field.Name, field.Name);
columns.Add(field.Name, "F");
}
}
// Value type
if (columns.Count == 0)
{
dgvList.Columns.Add(typeObj.Name, typeObj.Name);
columns.Add(typeObj.Name, "V");
}
// Retrieve the fields and properties values
foreach (object item in list)
{
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++;
}
dgvList.Rows.Add(row);
}
}
}
#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.