/// <summary> /// 分页查询,基础方法,参数:页面大小,页码,主键,查询字段,表达式,排序,数据总条数 /// </summary> /// <param name="pageSize">页面大小</param> /// <param name="pageIndex">页码</param> /// <param name="primaryKey">主键</param> /// <param name="selectFields">查询字段</param> /// <param name="express">表达式</param> /// <param name="orderBy">排序</param> /// <param name="recordCount">数据总条数</param> /// <returns></returns> private List <TEntity> Search(int pageSize, int pageIndex, string primaryKey, string selectFields, List <Expression> express, string orderBy, ref int recordCount) { //获取参数和条件 CoreFrameworkEntity CoreFrameworkEntity = GetParaListAndWhere(express); //条件 string where = CoreFrameworkEntity.where; //参数列表 List <SqlParameter> listPara = CoreFrameworkEntity.paraList; selectFields = selectFields == "" ? "*" : selectFields;//查询字段 Pager page = new Pager(); IDataReader sdr = page.GetPagerInfo(tableName, primaryKey, selectFields, pageSize, pageIndex, where, orderBy, ref recordCount, listPara); return(DynamicBuilder <TEntity> .GetList(sdr, columnAttrList)); }
/// <summary> /// 不分页查询,基础方法 /// </summary> /// <param name="count">显示总数,空则全部显示</param> /// <param name="selectFields">查询字段</param> /// <param name="express">表达式</param> /// <param name="orderBy">排序,为空则不排序</param> /// <returns></returns> private List <TEntity> Search(int?count, string selectFields, List <Expression> express, string orderBy) { //获取参数和条件 CoreFrameworkEntity CoreFrameworkEntity = GetParaListAndWhere(express); //条件 string where = CoreFrameworkEntity.where; //参数列表 List <SqlParameter> listPara = CoreFrameworkEntity.paraList; selectFields = selectFields == "" ? "*" : selectFields; //查询字段 orderBy = orderBy == "" ? "" : "order by " + orderBy; //排序 string topStr = count.HasValue == false ? "" : " top " + count + " "; string cmdText = "select " + topStr + selectFields + " from " + tableName + " where " + where + " " + orderBy; IDataReader sdr = new SqlConvertHelper().ExecuteReader(cmdText, listPara); return(DynamicBuilder <TEntity> .GetList(sdr, columnAttrList)); }
public static DynamicBuilder <T> CreateBuilder(IDataRecord dataRecord, List <EntityPropColumnAttributes> attributeList) { DynamicBuilder <T> dynamicBuilder = new DynamicBuilder <T>(); //定义一个名为DynamicCreate的动态方法,返回值typof(T),参数typeof(IDataRecord) DynamicMethod method = new DynamicMethod("DynamicCreate", typeof(T), new Type[] { typeof(IDataRecord) }, typeof(T), true); ILGenerator generator = method.GetILGenerator(); //创建一个MSIL生成器,为动态方法生成代码 LocalBuilder result = generator.DeclareLocal(typeof(T)); //声明指定类型的局部变量 可以T t;这么理解 //The next piece of code instantiates the requested type of object and stores it in the local variable. 可以t=new T();这么理解 generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < dataRecord.FieldCount; i++)//数据集合,熟悉的for循环 要干什么你懂的 { var list = attributeList.Where(w => w.fieldName.ToLower() == dataRecord.GetName(i).ToLower()); if (list.Count() > 0) { T t = System.Activator.CreateInstance <T>(); //typeof(T) PropertyInfo propertyInfo = t.GetType().GetProperty(list.First().propName); //根据列名取属性 原则上属性和列是一一对应的关系 Label endIfLabel = generator.DefineLabel(); if (propertyInfo != null && propertyInfo.GetSetMethod() != null)//实体存在该属性 且该属性有SetMethod方法 { /*The code then loops through the fields in the data reader, finding matching properties on the type passed in. * When a match is found, the code checks to see if the value from the data reader is null. */ generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod);//就知道这里要调用IsDBNull方法 如果IsDBNull==true contine generator.Emit(OpCodes.Brtrue, endIfLabel); /*If the value in the data reader is not null, the code sets the value on the object.*/ generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, getValueMethod); //调用get_Item方法 generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); //给该属性设置对应值 generator.MarkLabel(endIfLabel); //if (propertyInfo.PropertyType.FullName != "System.Guid") //{ //} //else //{ // generator.Emit(OpCodes.Ldloc, result); // generator.Emit(OpCodes.Ldarg_0); // generator.Emit(OpCodes.Ldc_I4, i); // generator.Emit(OpCodes.Callvirt, getGuidValueMethod);//调用get_Item方法 // generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); // generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());//给该属性设置对应值 // generator.MarkLabel(endIfLabel); // //propertyInfo.SetValue(t, Convert.ChangeType(propertyInfo.GetSetMethod(),propertyInfo.PropertyType),null); //} } } } /*The last part of the code returns the value of the local variable*/ generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret);//方法结束,返回 //完成动态方法的创建,并且创建执行该动态方法的委托,赋值到全局变量handler,handler在Build方法里Invoke dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return(dynamicBuilder); }