/// <summary> /// 更新实体 /// </summary> /// <param name="container">容器</param> /// <param name="model">实体</param> /// <param name="type">命名类型</param> public static void UpdateModel(Control container, object model, PrefixType type) { Control.ControlCollection ctrs = container.Controls; if (ctrs == null || ctrs.Count <= 0) { return; } ClassInfoHandle classHandle = ClassInfoManager.GetClassHandle(model.GetType()); foreach (Control ctr in ctrs) { string proName = UpdateModelInfo.GetKey(ctr.Name, type); if (string.IsNullOrEmpty(proName)) { continue; } PropertyInfoHandle handle = classHandle.PropertyInfo[proName]; if (handle == null || !handle.HasSetHandle)//搜索里层 { UpdateModel(ctr, model, type); } else { try { SetModelValue(ctr, model, handle); } catch { } } } }
/// <summary> /// 根据需要的数据获取反射信息 /// </summary> /// <param name="objType"></param> /// <param name="propertyCollection"></param> /// <returns></returns> private static List <EntitySerializerInfo> GetDicInfos(Type objType, IEnumerable <string> propertyCollection) { List <EntitySerializerInfo> lstInfos = new List <EntitySerializerInfo>(12); foreach (string strItem in propertyCollection) { string[] itemPart = strItem.Split('='); if (itemPart.Length < 2) { continue; } EntitySerializerInfo info = new EntitySerializerInfo(); info.Name = itemPart[0]; info.PropertyName = itemPart[1]; string[] propertyItems = itemPart[1].Split('.'); Type curType = objType; foreach (string proName in propertyItems) { PropertyInfoHandle pinfo = FastValueGetSet.GetPropertyInfoHandle(proName, curType); info.PropertyInfos.Add(pinfo); curType = pinfo.PropertyType; } lstInfos.Add(info); } return(lstInfos); }
private static readonly Type compareType = typeof(IComparable);//可比较对象的接口类型 /// <summary> /// 过滤集合 /// </summary> /// <param name="sourceList">源集合</param> /// <param name="lstScope">过滤条件</param> /// <returns></returns> public static IList RowFilter(IList sourceList, ScopeList lstScope) { if (sourceList.Count <= 0) { return(sourceList); } IList retLst = (IList)Activator.CreateInstance(sourceList.GetType()); Type objType = sourceList[0].GetType(); //初始化属性信息列表 List <CompareItemInfo> infos = new List <CompareItemInfo>(); foreach (Scope objScope in lstScope) { PropertyInfoHandle pInfo = FastValueGetSet.GetPropertyInfoHandle(objScope.PropertyName, objType); infos.Add(new CompareItemInfo(pInfo, objScope)); } foreach (object obj in sourceList) { List <CompareItem> lstCompareItem = new List <CompareItem>(); //Dictionary<string, Scope>.Enumerator enums = lstScope.GetEnumerator(); foreach (CompareItemInfo itemInfo in infos) { bool resault = IsAccord(itemInfo.PropertyInfo.GetValue(obj), itemInfo.ScopeInfo); lstCompareItem.Add(new CompareItem(resault, itemInfo.ScopeInfo.ConnectType)); } if (IsAllAccord(lstCompareItem)) { retLst.Add(obj); } } return(retLst); }
/// <summary> /// 收集集合里边所有对应属性的值 /// </summary> /// <param name="list">集合</param> /// <param name="propertyName">属性名</param> /// <returns></returns> public static ArrayList CollectValues(IEnumerable list, string propertyName) { ArrayList lst = new ArrayList(); PropertyInfoHandle pInfo = null; //PropertyInfoHandle pInfo = FastValueGetSet.GetPropertyInfoHandle(propertyName, objType); //if (pInfo == null) //{ // throw new Exception("找不到该属性"); //} foreach (object obj in list) { if (pInfo == null) { Type objType = obj.GetType(); pInfo = FastValueGetSet.GetPropertyInfoHandle(propertyName, objType); if (pInfo == null) { throw new Exception("找不到该属性"); } } lst.Add(pInfo.GetValue(obj)); } return(lst); }
/// <summary> /// 属性拷贝 /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void CpoyTo(object source, object target) { Dictionary <string, PropertyInfoHandle> dicSource = GetHandle(source.GetType()); Dictionary <string, PropertyInfoHandle> dicTarget = GetHandle(target.GetType()); PropertyInfoHandle tinfo = null; foreach (KeyValuePair <string, PropertyInfoHandle> kvp in dicSource) { string key = kvp.Key; if (dicTarget.TryGetValue(key, out tinfo)) { object value = kvp.Value.GetValue(source); try { if (kvp.Value.PropertyType != tinfo.PropertyType) { value = Convert.ChangeType(value, tinfo.PropertyType); } tinfo.SetValue(target, value); } catch { } } } }
//private static Dictionary<string, Dictionary<string, ModelInfo>> dicCache = new Dictionary<string, Dictionary<string, ModelInfo>>(); /// <summary> /// 填充实体跟页面的对应表 /// </summary> /// <param name="objPage">页面类</param> /// <param name="modleType">实体类型</param> private static Dictionary <string, ModelInfo> GetPageMapInfo(Control objControl, Type modleType, PrefixType pType) { Dictionary <string, ModelInfo> handleMapping = new Dictionary <string, ModelInfo>(); ClassInfoHandle handle = ClassInfoManager.GetClassHandle(modleType); List <Control> lstCtrs = GetAllContorl(objControl); foreach (Control ctr in lstCtrs) { string id = UpdateModelInfo.GetKey(ctr.ID, pType); if (!string.IsNullOrEmpty(id)) { ModelInfo info = new ModelInfo(); PropertyInfoHandle pInfo = handle.PropertyInfo[id]; if (pInfo != null) { info.Phandle = pInfo; ContorlDefaultPropertyInfo ctrInfo = ControlDefaultValue.GetDefaultPropertyInfoWithoutCache(ctr); info.CtrHandle = ctrInfo; info.Ctr = ctr; handleMapping[ctr.ID] = info; } } } return(handleMapping); }
/// <summary> /// 填充实体 /// </summary> /// <param name="dic">字典</param> /// <param name="model">实体</param> /// <param name="type">字典中的键类型</param> /// <param name="formatHandle">格式化信息</param> public static void UpdateModel(IDictionary <string, object> dic, object model, PrefixType type, DelUpdateModelFormatValue formatHandle) { Type objType = model.GetType(); foreach (KeyValuePair <string, object> kvp in dic) { string key = kvp.Key; string pName = UpdateModelInfo.GetKey(key, type); PropertyInfoHandle handle = FastValueGetSet.GetPropertyInfoHandle(pName, objType); if (handle != null && handle.HasSetHandle) { object value = kvp.Value; if (formatHandle != null) { value = formatHandle(handle, model, value); } else if (value.GetType() != handle.PropertyType) { value = Convert.ChangeType(value, handle.PropertyType); } handle.SetValue(model, value); } } }
public void Init(HttpApplication context) { PropertyInfoHandle fileChangesMonitorGet = FastValueGetSet.GetPropertyInfoHandle("FileChangesMonitor", typeof(HttpRuntime)); object o = fileChangesMonitorGet.GetValue(null); GetFieldValueHandle f = FastFieldGetSet.FindGetValueHandle(o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase)); object monitor = f(o); FastInvokeHandler m = FastValueGetSet.GetCustomerMethodInfo(monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic)); m.Invoke(monitor, new object[] { }); }
/// <summary> /// 查找实体的关联属性信息属性 /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public BQLEntityTableHandle FindChildEntity(string propertyName) { BQLEntityTableHandle table = null; PropertyInfoHandle handle = FastValueGetSet.GetPropertyInfoHandle(propertyName, this.GetType()); if (!handle.HasGetHandle) { throw new MissingMemberException("不存在属性:" + propertyName); } table = handle.GetValue(this) as BQLEntityTableHandle; return(table); }
/// <summary> /// 给模型赋值 /// </summary> /// <param name="ctr">控件</param> /// <param name="model">模型</param> /// <param name="handle">属性信息</param> private static void SetModelValue(Control ctr, object model, PropertyInfoHandle handle) { object value = ControlDefaultValue.GetControlDefaultPropertyValue(ctr); if (value == null) { return; } object readValue = CommonMethods.EntityProChangeType(value, handle.PropertyType); handle.SetValue(model, readValue); }
/// <summary> /// 根据排序方式列表获取对象的Get属性句柄和对应排序方式 /// </summary> /// <param name="obj"></param> /// <param name="lstSort"></param> /// <returns></returns> private static List <SortCompartItem> GetSortComparts(object obj, SortList lstSort) { Type type = obj.GetType(); List <SortCompartItem> lstSCompare = new List <SortCompartItem>(); foreach (Sort objSort in lstSort) { PropertyInfoHandle getHandle = FastValueGetSet.GetPropertyInfoHandle(objSort.PropertyName, type); SortCompartItem item = new SortCompartItem(); item.CurSortType = objSort.SortType; item.GetValueHandler = getHandle; lstSCompare.Add(item); } return(lstSCompare); }
/// <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); }
/// <summary> /// 根据集合里边对应属性的值组合出Dictionary /// </summary> /// <param name="list">集合</param> /// <param name="propertyName">属性名</param> /// <returns></returns> public static Dictionary <T, K> CollectValues <T, K>(IEnumerable <K> list, string propertyName) { Dictionary <T, K> dic = new Dictionary <T, K>(); Type objType = typeof(K); PropertyInfoHandle pInfo = FastValueGetSet.GetPropertyInfoHandle(propertyName, objType); if (pInfo == null) { throw new Exception("找不到该属性"); } foreach (K obj in list) { dic[(T)pInfo.GetValue(obj)] = obj; } return(dic); }
/// <summary> /// 根据反射信息获取值(链式) /// </summary> /// <param name="info"></param> /// <param name="obj"></param> /// <returns></returns> private static bool SetValue(EntitySerializerInfo info, object obj, object value) { object tmpObj = obj; int hcount = info.PropertyInfos.Count; if (hcount > 1) { for (int i = 0; i < hcount - 1; i++) { PropertyInfoHandle pHandle = info.PropertyInfos[i]; tmpObj = pHandle.GetValue(value); if (tmpObj == null) { return(false); } } } PropertyInfoHandle sethandle = info.PropertyInfos[hcount - 1]; sethandle.SetValue(tmpObj, value); return(true); }
/// <summary> /// 从流中读取XML /// </summary> /// <param name="stm">流</param> /// <returns></returns> public static List <T> ReadXML <T>(Stream stm, PageContent objPage) { stm.Position = 0; Type type = typeof(T); List <T> lst = new List <T>(); XmlDocument doc = new XmlDocument(); doc.Load(stm); if (objPage != null) //读取分页信息 { XmlNodeList rootList = doc.GetElementsByTagName("root"); if (rootList.Count > 0) { XmlAttribute attTotalPage = rootList[0].Attributes["totalPage"]; XmlAttribute attCurrentPage = rootList[0].Attributes["currentPage"]; XmlAttribute attPageSize = rootList[0].Attributes["pageSize"]; if (attTotalPage != null) { objPage.TotalPage = Convert.ToInt32(attTotalPage.InnerText); } if (attCurrentPage != null) { objPage.CurrentPage = Convert.ToInt32(attCurrentPage.InnerText); } if (attPageSize != null) { objPage.PageSize = Convert.ToInt32(attPageSize.InnerText); } } } ClassInfoHandle entityInfo = null; if (!type.IsValueType) { entityInfo = ClassInfoManager.GetClassHandle(type); } XmlNodeList nodeList = doc.GetElementsByTagName("item"); foreach (XmlNode node in nodeList) { if (entityInfo != null) { T obj = (T)entityInfo.CreateInstance(); foreach (XmlNode itemNode in node.ChildNodes) { string tagName = itemNode.Name; string value = itemNode.InnerText; PropertyInfoHandle info = entityInfo.PropertyInfo[tagName]; if (info != null) { Type resType = info.PropertyType; object curValue = StringToValue(value, resType); //转换成对象的值 info.SetValue(obj, curValue); //赋值 } } lst.Add(obj); } else { T curValue = (T)StringToValue(node.InnerText, type);//转换成对象的值 lst.Add(curValue); } } return(lst); }
public CompareItemInfo(PropertyInfoHandle pInfo, Scope objScope) { this.pInfo = pInfo; this.objScope = objScope; }