在程序中动态生成实体类
Code
public class DataColumnInfo
{
public string ColumnName { get; set; }
public System.Type DataType { get; set; }
}
public class DynamicDataBuilder
{
public static DynamicDataBuilder Instance = new DynamicDataBuilder(); // Make it singleton class, so easier to use
private ModuleBuilder moduleBuilder;
private System.Type BuildDataObjectType(List<DataColumnInfo> Columns, string DataObjectName)
{
if (moduleBuilder == null)
{
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("DynamicData"), AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule("DataModule");
}
TypeBuilder tb = moduleBuilder.DefineType(DataObjectName,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object));
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
foreach (var info in Columns)
{
string propertyName = info.ColumnName.Replace(' ', '_');
FieldBuilder fb = tb.DefineField("_" + propertyName, info.DataType, FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, info.DataType, null);
MethodBuilder getMethod = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
info.DataType,
Type.EmptyTypes);
ILGenerator ilgen = getMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetGetMethod(getMethod);
MethodBuilder setMethod = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
null,
new Type[] { info.DataType });
ilgen = setMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldarg_1);
ilgen.Emit(OpCodes.Stfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetSetMethod(setMethod);
}
System.Type t = tb.CreateType();
return t;
}
public List<object> GetDataList(string XML)
{
// Read XML, build ColumnInfo List or convert it to your DataTable Object
List<DataColumnInfo> Columns = new List<DataColumnInfo>();
// Here are just some testing code so you can see how it works
Columns.Add(new DataColumnInfo { ColumnName = "Title", DataType = typeof(string) });
Columns.Add(new DataColumnInfo { ColumnName = "Descrption", DataType = typeof(string) });
Columns.Add(new DataColumnInfo { ColumnName = "StartDate", DataType = typeof(DateTime) });
Columns.Add(new DataColumnInfo { ColumnName = "EndDate", DataType = typeof(DateTime) });
System.Type dataType = BuildDataObjectType(Columns, "Data"); // Build the Dynamic Data Type
List<object> list = new List<object>();
for (int i = 0; i < 10; i++)
{
object Data = Activator.CreateInstance(dataType);
if (Data != null)
{
PropertyInfo pi = dataType.GetProperty("Title"); // Read your XML to fill each field of the Dynamic Data
if (pi != null)
pi.SetValue(Data, "TEST" + i, null);
pi = dataType.GetProperty("StartDate");
if (pi != null)
pi.SetValue(Data, System.DateTime.Now, null);
}
list.Add(Data); // add the DataObject to the List
}
return list;
}
}
//Bind XMLData to the DataGrid:
theGrid.ItemsSource = DynamicDataBuilder.Instance.GetDataList(xmlData);
//
public class DataColumnInfo
{
public string ColumnName { get; set; }
public System.Type DataType { get; set; }
}
public class DynamicDataBuilder
{
public static DynamicDataBuilder Instance = new DynamicDataBuilder(); // Make it singleton class, so easier to use
private ModuleBuilder moduleBuilder;
private System.Type BuildDataObjectType(List<DataColumnInfo> Columns, string DataObjectName)
{
if (moduleBuilder == null)
{
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("DynamicData"), AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule("DataModule");
}
TypeBuilder tb = moduleBuilder.DefineType(DataObjectName,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object));
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
foreach (var info in Columns)
{
string propertyName = info.ColumnName.Replace(' ', '_');
FieldBuilder fb = tb.DefineField("_" + propertyName, info.DataType, FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, info.DataType, null);
MethodBuilder getMethod = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
info.DataType,
Type.EmptyTypes);
ILGenerator ilgen = getMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetGetMethod(getMethod);
MethodBuilder setMethod = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
null,
new Type[] { info.DataType });
ilgen = setMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldarg_1);
ilgen.Emit(OpCodes.Stfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetSetMethod(setMethod);
}
System.Type t = tb.CreateType();
return t;
}
public List<object> GetDataList(string XML)
{
// Read XML, build ColumnInfo List or convert it to your DataTable Object
List<DataColumnInfo> Columns = new List<DataColumnInfo>();
// Here are just some testing code so you can see how it works
Columns.Add(new DataColumnInfo { ColumnName = "Title", DataType = typeof(string) });
Columns.Add(new DataColumnInfo { ColumnName = "Descrption", DataType = typeof(string) });
Columns.Add(new DataColumnInfo { ColumnName = "StartDate", DataType = typeof(DateTime) });
Columns.Add(new DataColumnInfo { ColumnName = "EndDate", DataType = typeof(DateTime) });
System.Type dataType = BuildDataObjectType(Columns, "Data"); // Build the Dynamic Data Type
List<object> list = new List<object>();
for (int i = 0; i < 10; i++)
{
object Data = Activator.CreateInstance(dataType);
if (Data != null)
{
PropertyInfo pi = dataType.GetProperty("Title"); // Read your XML to fill each field of the Dynamic Data
if (pi != null)
pi.SetValue(Data, "TEST" + i, null);
pi = dataType.GetProperty("StartDate");
if (pi != null)
pi.SetValue(Data, System.DateTime.Now, null);
}
list.Add(Data); // add the DataObject to the List
}
return list;
}
}
//Bind XMLData to the DataGrid:
theGrid.ItemsSource = DynamicDataBuilder.Instance.GetDataList(xmlData);
//
在silverlight的datagrid中绑定
//////////////////////////////////////例子 在windows中
Code
namespace WindowsFormsApplication1
{
public class DynamicDataBuilder
{
public static DynamicDataBuilder Instance = new DynamicDataBuilder(); // Make it singleton class, so easier to use
private ModuleBuilder moduleBuilder;
public System.Type BuildDataObjectType(List<DataColumnInfo> Columns, string DataObjectName)
{
if (moduleBuilder == null)
{
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("DynamicData"), AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule("DataModule");
}
TypeBuilder tb = moduleBuilder.DefineType(DataObjectName,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object));
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
foreach (var info in Columns)
{
string propertyName = info.ColumnName.Replace(' ', '_');
FieldBuilder fb = tb.DefineField("_" + propertyName, info.DataType, FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, info.DataType, null);
MethodBuilder getMethod = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
info.DataType,
Type.EmptyTypes);
ILGenerator ilgen = getMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetGetMethod(getMethod);
MethodBuilder setMethod = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
null,
new Type[] { info.DataType });
ilgen = setMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldarg_1);
ilgen.Emit(OpCodes.Stfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetSetMethod(setMethod);
}
System.Type t = tb.CreateType();
return t;
}
}
public class DataColumnInfo
{
public string ColumnName { get; set; }
public System.Type DataType { get; set; }
}
}
////////////////////////////////////////////
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
DataColumn dc1 = new DataColumn("col1");
dt1.Columns.Add(dc1);
DataRow row1 = dt1.NewRow();
row1["col1"] = "a";
dt1.Rows.Add(row1);
DataTable dt2 = new DataTable();
DataColumn dc2 = new DataColumn("col2");
dt2.Columns.Add(dc2);
DataRow row2 = dt2.NewRow();
row2["col2"] = "b";
dt2.Rows.Add(row2);
//dt1.Columns.Add(dc2);
//dt1.Merge(dt2, false, MissingSchemaAction.Ignore );
//DataTable dt3=UniteDataTable(dt1,dt2,"table3");
//dt1 = UniteDataTable(dt1, dt2, "Tableaa");
List<DataColumnInfo> Columns = new List<DataColumnInfo>();
foreach (DataColumn col in dt1.Columns)
{
Columns.Add(new DataColumnInfo { ColumnName = col.ColumnName, DataType = typeof(string) });
}
DynamicDataBuilder dy = new DynamicDataBuilder();
System.Type dataType = dy.BuildDataObjectType(Columns, "DyData");
List<object> list = new List<object>();
for (int i = 0; i <dt1.Rows.Count; i++)
{
object Data = Activator.CreateInstance(dataType);
if (Data != null)
{
string colName="col1";
PropertyInfo pi = dataType.GetProperty(colName); // Read your XML to fill each field of the Dynamic Data
if (pi != null)
pi.SetValue(Data, dt1.Rows[i][colName], null);
pi = dataType.GetProperty("col2");
if (pi != null)
pi.SetValue(Data, System.DateTime.Now.ToShortDateString(), null);
}
list.Add(Data); // add the DataObject to the List
}
//return list;
//foreach (DataRow dr in dt1.Rows)
//{
// object o= ConvertToEntity(dr, typeof("aaa"));
//}
//List<object[]> list = new List<object[]>();
//foreach(DataRow dr in dt1.Rows)
//{
//object[] obj = new object[dt1.Columns.Count];
// dr.ItemArray.CopyTo(obj, 0);
// list.Add(obj);
//}
//DataColumn
//object[] o = new object[dt1.Columns.Count];
//o[0] = "Col1";
//o[1] = "col2";
//object[] o = new object[dt1.Columns.Count];
//for (int i = 0; i < dt1.Columns.Count; i++)
//{
// o[i] = dt1.Columns[i].ColumnName;
//}
//list.Insert(0, o);
}
}
namespace WindowsFormsApplication1
{
public class DynamicDataBuilder
{
public static DynamicDataBuilder Instance = new DynamicDataBuilder(); // Make it singleton class, so easier to use
private ModuleBuilder moduleBuilder;
public System.Type BuildDataObjectType(List<DataColumnInfo> Columns, string DataObjectName)
{
if (moduleBuilder == null)
{
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("DynamicData"), AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule("DataModule");
}
TypeBuilder tb = moduleBuilder.DefineType(DataObjectName,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object));
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
foreach (var info in Columns)
{
string propertyName = info.ColumnName.Replace(' ', '_');
FieldBuilder fb = tb.DefineField("_" + propertyName, info.DataType, FieldAttributes.Private);
PropertyBuilder pb = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, info.DataType, null);
MethodBuilder getMethod = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
info.DataType,
Type.EmptyTypes);
ILGenerator ilgen = getMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetGetMethod(getMethod);
MethodBuilder setMethod = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
null,
new Type[] { info.DataType });
ilgen = setMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldarg_1);
ilgen.Emit(OpCodes.Stfld, fb);
ilgen.Emit(OpCodes.Ret);
pb.SetSetMethod(setMethod);
}
System.Type t = tb.CreateType();
return t;
}
}
public class DataColumnInfo
{
public string ColumnName { get; set; }
public System.Type DataType { get; set; }
}
}
////////////////////////////////////////////
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
DataColumn dc1 = new DataColumn("col1");
dt1.Columns.Add(dc1);
DataRow row1 = dt1.NewRow();
row1["col1"] = "a";
dt1.Rows.Add(row1);
DataTable dt2 = new DataTable();
DataColumn dc2 = new DataColumn("col2");
dt2.Columns.Add(dc2);
DataRow row2 = dt2.NewRow();
row2["col2"] = "b";
dt2.Rows.Add(row2);
//dt1.Columns.Add(dc2);
//dt1.Merge(dt2, false, MissingSchemaAction.Ignore );
//DataTable dt3=UniteDataTable(dt1,dt2,"table3");
//dt1 = UniteDataTable(dt1, dt2, "Tableaa");
List<DataColumnInfo> Columns = new List<DataColumnInfo>();
foreach (DataColumn col in dt1.Columns)
{
Columns.Add(new DataColumnInfo { ColumnName = col.ColumnName, DataType = typeof(string) });
}
DynamicDataBuilder dy = new DynamicDataBuilder();
System.Type dataType = dy.BuildDataObjectType(Columns, "DyData");
List<object> list = new List<object>();
for (int i = 0; i <dt1.Rows.Count; i++)
{
object Data = Activator.CreateInstance(dataType);
if (Data != null)
{
string colName="col1";
PropertyInfo pi = dataType.GetProperty(colName); // Read your XML to fill each field of the Dynamic Data
if (pi != null)
pi.SetValue(Data, dt1.Rows[i][colName], null);
pi = dataType.GetProperty("col2");
if (pi != null)
pi.SetValue(Data, System.DateTime.Now.ToShortDateString(), null);
}
list.Add(Data); // add the DataObject to the List
}
//return list;
//foreach (DataRow dr in dt1.Rows)
//{
// object o= ConvertToEntity(dr, typeof("aaa"));
//}
//List<object[]> list = new List<object[]>();
//foreach(DataRow dr in dt1.Rows)
//{
//object[] obj = new object[dt1.Columns.Count];
// dr.ItemArray.CopyTo(obj, 0);
// list.Add(obj);
//}
//DataColumn
//object[] o = new object[dt1.Columns.Count];
//o[0] = "Col1";
//o[1] = "col2";
//object[] o = new object[dt1.Columns.Count];
//for (int i = 0; i < dt1.Columns.Count; i++)
//{
// o[i] = dt1.Columns[i].ColumnName;
//}
//list.Insert(0, o);
}
}
测试通过
Tuesday, August 04, 2009 15:12:00