/// <summary> /// 把集合转换成字典类 /// </summary> /// <typeparam name="TKey">键类型</typeparam> /// <typeparam name="TValue">值类型 </typeparam> /// <param name="collection">集合类</param> /// <param name="keyProperty">键名</param> /// <returns></returns> public static Dictionary <TKey, TValue> ListToDictionary <TKey, TValue>(IEnumerable collection, string keyProperty) { Dictionary <TKey, TValue> dic = new Dictionary <TKey, TValue>(); FastPropertyHandler handle = FastValueGetSet.GetGetMethodInfo(keyProperty, typeof(TValue)); object[] emptyParams = new object[] { }; foreach (TValue objValue in collection) { object obj = objValue; if (obj == null) { continue; } TKey key = default(TKey); object objKey = handle(obj, emptyParams); if (typeof(TKey) == DefaultType.StringType) { object strkey = objKey.ToString(); dic[(TKey)strkey] = objValue; } else { key = (TKey)objKey; dic[key] = objValue; } } return(dic); }
/// <summary> /// 初始化数据库 /// </summary> public static void InitDB() { //if (_isInit) //{ // return; //} Type type = typeof(T); DataAccessLoader.AppendModelAssembly(type.Assembly); DataAccessLoader.InitConfig(); _db = GetDB(); Type baseType = typeof(BQLEntityTableHandle); PropertyInfo[] infos = type.GetProperties(BindingFlags.Public | BindingFlags.Static); foreach (PropertyInfo info in infos) { Type objType = info.PropertyType; if (!objType.IsSubclassOf(baseType)) { continue; } BQLEntityTableHandle handle = FastValueGetSet.GetGetMethodInfo(info.Name, type).Invoke(null, new object[] { }) as BQLEntityTableHandle; AddToDB(handle); } StaticConnection.ClearCacheOperate(_db); #if DEBUG _db.SqlOutputer.OnOutputerCreate += new Buffalo.DB.MessageOutPuters.CreateOutputerHandle(SqlOutputer_OnOutputerCreate); #endif }
/// <summary> /// 初始化类型的属性信息 /// </summary> /// <param name="type">类型</param> /// <returns>如果已经初始化过侧返回false</returns> private static void InitClassPropertyInfos(Type type) { string fullName = type.FullName; //实例化本类型的句柄 CreateInstanceHandler createrHandel = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type); Dictionary <string, PropertyInfoHandle> dicPropertys = new Dictionary <string, PropertyInfoHandle>(); Dictionary <string, FieldInfoHandle> dicField = new Dictionary <string, FieldInfoHandle>(); //属性信息句柄 PropertyInfo[] destproper = type.GetProperties(FastValueGetSet.AllBindingFlags); FieldInfo[] allField = type.GetFields(FastValueGetSet.AllBindingFlags); //int index = 0; ///读取属性别名 foreach (PropertyInfo pinf in destproper) { ///通过属性来反射 string proName = pinf.Name; FastPropertyHandler getHandle = FastValueGetSet.GetGetMethodInfo(proName, type); FastPropertyHandler setHandle = FastValueGetSet.GetSetMethodInfo(proName, type); if (getHandle != null || setHandle != null) { PropertyInfoHandle classProperty = new PropertyInfoHandle(type, getHandle, setHandle, pinf.PropertyType, pinf.Name); dicPropertys.Add(pinf.Name, classProperty); } } ///读取属性别名 foreach (FieldInfo fInf in allField) { string proName = fInf.Name; GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(fInf); SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(fInf); if (getHandle != null || setHandle != null) { FieldInfoHandle fieldInfo = new FieldInfoHandle(type, getHandle, setHandle, fInf.FieldType, fInf.Name, fInf); dicField.Add(fInf.Name, fieldInfo); } } ClassInfoHandle classInfo = new ClassInfoHandle(type, createrHandel, dicPropertys, dicField); dicClass.Add(fullName, classInfo); }