public static DataTableBuilder <T> CreateBuilder(DataRow dataRecord) { var generator = new DynamicMethod("DynamicCreateEntity", typeof(T), new[] { typeof(DataRow) }, typeof(T), true).GetILGenerator(); var result = generator.DeclareLocal(typeof(T)); generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < dataRecord.ItemArray.Length; i++) { var propertyInfo = typeof(T).GetProperty(dataRecord.Table.Columns[i].ColumnName); var endIfLabel = generator.DefineLabel(); if (propertyInfo == null || propertyInfo.GetSetMethod() == null) { continue; } 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, propertyInfo.PropertyType); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); generator.MarkLabel(endIfLabel); } generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret); return(new DataTableBuilder <T> { _handler = (Load) new DynamicMethod("DynamicCreateEntity", typeof(T), new[] { typeof(DataRow) }, typeof(T), true).CreateDelegate(typeof(Load)) }); }