To generate a properties at run-time and using pre-defined class.
using
System;
using
System.Data;
using
System.Collections.Generic;
namespace
ReportEngine
{
class Program
{
static void Main(string[]
args)
{
DataTable
table = InputGeneration.GetTable();
//InputGeneration.DisplayTable(table);
#region "Method1 - Predefined
class and generate properties alone at runtime"
Console.WriteLine("\n---Method1 - Predefined class and generate
properties alone at runtime------\n");
List<string> PropertyNames = Method1.GenerateProperties(table);
ListDynamicClass > DynamicClassList = Method1.GenerateClass(PropertyNames, table);
Method1.DisplayClass(DynamicClassList,
PropertyNames);
#endregion
Console.WriteLine("\n---------------------------------------------------------\n");
Console.ReadLine();
}
}
}
InputGeneration:
using System;
using System.Data;
namespace ReportEngine
{
public static class InputGeneration
{
public static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new
DataTable();
table.Columns.Add("Dosage",
typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient",
typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin",
"David", DateTime.Now);
table.Rows.Add(50, "Enebrel",
"Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine",
"Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent",
"Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin",
"Melanie", DateTime.Now);
return table;
}
public static void DisplayTable(DataTable
dt)
{
if (dt.Rows.Count > 0)
{
foreach (DataColumn
column in dt.Columns)
{
Console.Write("\t{0}\t",
column.ColumnName);
}
Console.WriteLine("\t");
foreach (DataRow
row in dt.Rows)
{
foreach (DataColumn
column in dt.Columns)
Console.Write("\t{0}\t", row[column]);
Console.WriteLine("\t");
}
}
else
Console.WriteLine("No
Current Rows Found");
}
}
}
Method1:
using System;
using System.Data;
using System.Collections.Generic;
using ReportEngine.PropertyAtRuntime;
namespace ReportEngine
{
public static class Method1
{
public static List<string>
GenerateProperties(DataTable table)
{
List<string>
PropertyNames = new List<string>();
foreach (DataColumn
column in table.Columns)
{
PropertyNames.Add(column.ColumnName.ToUpper());
}
return PropertyNames;
}
public static List<DynamicClass>
GenerateClass(List<string> PropertyNames, DataTable
table)
{
List<DynamicClass>
DynamicClassList = new List<DynamicClass>();
foreach (DataRow
row in table.Rows)
{
var dynamicClass = new
DynamicClass();
foreach (string
PropertyName in PropertyNames)
{
dynamicClass.property[PropertyName] = row[PropertyName].ToString();
}
DynamicClassList.Add(dynamicClass);
}
return DynamicClassList;
}
public static void DisplayClass(List<DynamicClass> DynamicClassList, List<string>
PropertyNames)
{
foreach (string
PropertyName in PropertyNames)
{
Console.Write("{0}\t",
PropertyName);
}
Console.WriteLine("\n");
foreach (DynamicClass
dynamicClass in DynamicClassList)
{
foreach (string
PropertyName in PropertyNames)
{
Console.Write("{0}\t",
dynamicClass.property[PropertyName]);
}
Console.WriteLine("\n");
}
}
}
}
DynamicClass:
using
System;
namespace
ReportEngine.PropertyAtRuntime
{
public class DynamicClass
{
// property
is a class that will create dynamic properties at runtime
private
DynamicProperty _property = new DynamicProperty();
public DynamicProperty property
{
get
{ return _property; }
set
{ _property = value; }
}
}
}
DynamicProperty:
using
System;
using
System.Collections.Generic;
namespace
ReportEngine.PropertyAtRuntime
{
public class DynamicProperty
{
// a
Dictionary that hold all the dynamic property values
private
Dictionary<string,
object> properties = new Dictionary<string, object>();
// the
property call to get any dynamic property in our Dictionary, or "" if
none found.
public object this[string name]
{
get
{
if
(properties.ContainsKey(name))
{
return
properties[name];
}
return
"";
}
set
{
properties[name] = value;
}
}
}
}
No comments:
Post a Comment