public static DataTableEntityBuilder <T> CreateBuilder(DataRow dr) { DataTableEntityBuilder <T> dynamicBuilder = new DataTableEntityBuilder <T>(); DynamicMethod method = new DynamicMethod("DynamicCreateEntity", typeof(T), new Type[] { typeof(DataRow) }, typeof(T), true); ILGenerator generator = method.GetILGenerator(); LocalBuilder result = generator.DeclareLocal(typeof(T)); generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < dr.ItemArray.Length; i++) { PropertyInfo pi = typeof(T).GetProperty(dr.Table.Columns[i].ColumnName); Label endIfLabel = generator.DefineLabel(); if (pi != null && pi.GetSetMethod() != null) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod); generator.Emit(OpCodes.Brtrue, endIfLabel); generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, getValueMethod); generator.Emit(OpCodes.Unbox_Any, pi.PropertyType); generator.Emit(OpCodes.Callvirt, pi.GetSetMethod()); generator.MarkLabel(endIfLabel); } } generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret); dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return(dynamicBuilder); }
public static T GetEntity <T>(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { return(default(T)); } var dr = dt.Rows[0]; DataTableEntityBuilder <T> eblist = DataTableEntityBuilder <T> .CreateBuilder(dr); return(eblist.Build(dr)); }
public static List <T> GetList <T>(DataTable dt) { List <T> lst = new List <T>(); if (dt == null || dt.Rows.Count == 0) { return(lst); } DataTableEntityBuilder <T> eblist = DataTableEntityBuilder <T> .CreateBuilder(dt.Rows[0]); foreach (DataRow dr in dt.Rows) { lst.Add(eblist.Build(dr)); } dt.Dispose(); dt = null; return(lst); }
public static T GetEntity <T>(DataRow dr) { DataTableEntityBuilder <T> eblist = DataTableEntityBuilder <T> .CreateBuilder(dr); return(eblist.Build(dr)); }