/// <summary> /// 返回单个实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <returns></returns> public TEntity ToFirstDefault <TEntity>() where TEntity : Entity { TEntity t = ToFirst <TEntity>(); if (t == null) { t = DataUtils.Create <TEntity>(); } return(t); }
/// <summary> /// 返回清空Web页面的值的脚本 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="prefix"></param> public static string SetDocumentValueClearString <TEntity>(string prefix) where TEntity : Entity { StringBuilder script = new StringBuilder(); script.AppendLine(); TEntity t = DataUtils.Create <TEntity>(); Dictionary <Field, object> fieldvalues = DataUtils.FieldValueToDictionary(t.GetFields(), t.GetValues()); foreach (KeyValuePair <Field, object> fieldvalue in fieldvalues) { string varname = prefix + fieldvalue.Key.PropertyName; script.Append("var "); script.Append(varname); script.Append("=document.getElementById('"); script.Append(varname); script.Append("');if("); script.Append(varname); script.Append(")"); if (null == fieldvalue.Value || Convert.IsDBNull(fieldvalue.Value)) { script.Append(varname); script.Append(".value='';"); } else { Type valueType = fieldvalue.Value.GetType(); if (valueType == typeof(bool)) { script.Append("{try{"); script.Append(varname); script.Append(".checked=false"); script.Append(";}catch(err){"); script.Append(varname); script.Append(".value='0'}}"); } else { script.Append(varname); script.Append(".value='';"); } } } return(script.ToString()); }
/// <summary> /// 返回T /// </summary> /// <returns></returns> private static TEntity getTEntity <TEntity>() where TEntity : Entity { var typestring = typeof(TEntity).ToString(); if (_entityList.ContainsKey(typestring)) { return((TEntity)_entityList[typestring]); } lock (LockObj) { if (_entityList.ContainsKey(typestring)) { return((TEntity)_entityList[typestring]); } var t = DataUtils.Create <TEntity>(); _entityList.Add(typestring, t); return(t); } }
/// <summary> /// 实体赋值 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="toEntity">被赋值实体</param> /// <param name="fromEntity">来源实体</param> /// <returns>返回null 就表示fromEntity==null</returns> public static void SetValue <TEntity>(TEntity toEntity, TEntity fromEntity) where TEntity : Entity { if (null == fromEntity) { toEntity = null; } else { if (null == toEntity) { toEntity = DataUtils.Create <TEntity>(); } toEntity.Attach(); PropertyInfo[] pis = toEntity.GetType().GetProperties(); foreach (PropertyInfo pi in pis) { DataUtils.SetPropertyValue(toEntity, pi, DataUtils.GetPropertyValue <TEntity>(fromEntity, pi.Name)); } } }
public static List <T> Map <T>(IDataReader reader) { if (reader == null || reader.IsClosed) { throw new Exception("连接已关闭!"); } #region Not use the cache for the time being 2015-06-14 //if (m_CatchMethod == null) //{ // m_CatchMethod = new Dictionary<string, DynamicMethod>(); //} #endregion Type itemType = typeof(T); T t = DataUtils.Create <T>(); var key = itemType.FullName; //if (!m_CatchMethod.ContainsKey(key))//Not use the cache for the time being 2015-06-14 //{ #if WRITE_FILE AssemblyName aName = new AssemblyName("DynamicAssembly"); AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll"); TypeBuilder tb = mb.DefineType("DynamicType", TypeAttributes.Public); #endif Type listType = typeof(List <T>); Type objectType = typeof(object); Type objArrayType = typeof(object[]); Type boolType = typeof(bool); Type[] methodArgs = { typeof(IDataReader) }; MethodInfo LAdd = listType.GetMethod("Add"); PropertyInfo[] properties = null; getMapped(t, itemType, reader, out properties); #if WRITE_FILE MethodBuilder dm = tb.DefineMethod("ReadEntities", MethodAttributes.Public | MethodAttributes.Static, listType, methodArgs); #else DynamicMethod dm = new DynamicMethod("ReadEntities", listType, methodArgs, typeof(Mapper)); #endif ILGenerator ilg = dm.GetILGenerator(); //List<T> list; LocalBuilder list = ilg.DeclareLocal(listType); //T item; LocalBuilder item = ilg.DeclareLocal(itemType); //object[] values; LocalBuilder values = ilg.DeclareLocal(objArrayType); //object objValue; LocalBuilder objValue = ilg.DeclareLocal(objectType); //type nulls LocalBuilder[] typeNulls = null; initNulls(properties, ilg, out typeNulls); Label exit = ilg.DefineLabel(); Label loop = ilg.DefineLabel(); Label[] lblArray = new Label[properties.Length * 2]; for (int i = 0; i < lblArray.Length; i++) { lblArray[i] = ilg.DefineLabel(); } //list = new List<T>(); ilg.Emit(OpCodes.Newobj, listType.GetConstructor(Type.EmptyTypes)); ilg.Emit(OpCodes.Stloc_S, list); //values=new object[FieldCount]; ilg.Emit(OpCodes.Ldc_I4, reader.FieldCount); ilg.Emit(OpCodes.Newarr, objectType); ilg.Emit(OpCodes.Stloc_S, values); // while (arg.Read()) { ilg.MarkLabel(loop); ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Callvirt, Reader_Read); ilg.Emit(OpCodes.Brfalse, exit); //reader.GetValues(values); ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Ldloc_S, values); ilg.Emit(OpCodes.Callvirt, Reader_GetValues); ilg.Emit(OpCodes.Pop); //item=new T(); ilg.Emit(OpCodes.Newobj, itemType.GetConstructor(Type.EmptyTypes)); ilg.Emit(OpCodes.Stloc_S, item); //item.Property=Convert(values[index]); for (int index = 0; index < properties.Length; index++) { PropertyInfo pi = properties[index]; if (pi == null) { continue; } //objValue=value[index]; ilg.Emit(OpCodes.Ldloc_S, values); ilg.Emit(OpCodes.Ldc_I4, index); ilg.Emit(OpCodes.Ldelem_Ref); ilg.Emit(OpCodes.Stloc_S, objValue); //tmpBool=Convert.IsDBNull(objValue); ilg.Emit(OpCodes.Ldloc_S, objValue); ilg.Emit(OpCodes.Call, Convert_IsDBNull); //if (!tmpBool){ ilg.Emit(OpCodes.Brtrue_S, lblArray[index * 2]); //item.Field=Convert(objValue).ToXXX(); ilg.Emit(OpCodes.Ldloc_S, item); ilg.Emit(OpCodes.Ldloc_S, objValue); convertValue(ilg, pi); ilg.Emit(OpCodes.Callvirt, pi.GetSetMethod()); //} ilg.Emit(OpCodes.Br_S, lblArray[index * 2 + 1]); //else { ilg.MarkLabel(lblArray[index * 2]); //item.Field=objValue; ilg.Emit(OpCodes.Ldloc_S, item); ilg.Emit(OpCodes.Ldloc_S, typeNulls[index]); ilg.Emit(OpCodes.Callvirt, pi.GetSetMethod()); //} ilg.MarkLabel(lblArray[index * 2 + 1]); } //list.Add(item); ilg.Emit(OpCodes.Ldloc_S, list); ilg.Emit(OpCodes.Ldloc_S, item); ilg.Emit(OpCodes.Callvirt, LAdd); //} ilg.Emit(OpCodes.Br, loop); ilg.MarkLabel(exit); // return list; ilg.Emit(OpCodes.Ldloc_S, list); ilg.Emit(OpCodes.Ret); #if WRITE_FILE Type t = tb.CreateType(); ab.Save(aName.Name + ".dll"); #else //m_CatchMethod.Add(key, dm);//Not use the cache for the time being 2015-06-14 #endif //if (m_CatchMethod.Count > 100)Not use the cache for the time being 2015-06-14 { } //} //if (m_CatchMethod.ContainsKey(key))Not use the cache for the time being 2015-06-14 //{ //DynamicMethod dm = m_CatchMethod[key];//Not use the cache for the time being 2015-06-14 ReadEntityInvoker <List <T> > invoker = dm.CreateDelegate(typeof(ReadEntityInvoker <List <T> >)) as ReadEntityInvoker <List <T> >; try// 2015-07-02 { return(invoker.Invoke(reader)); } catch (Exception) { reader.Close(); throw; } //} throw new Exception("没有找到对应类型的处理方法。"); }