private static void SetPropertyValueByElementList(object t, PropertyInfo propertyInfo, XmlNodeList xmlNodeList, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { Type itemType = ReflectionHelper.GetListGenericType(propertyInfo); Dictionary <PropertyInfo, XmlTAttribute> propertyNameDict = InitXmlToEntityMapper(itemType); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict(itemType, reflectionType); } var objectDataList = ReflectionHelper.NewList(itemType) as IList; foreach (XmlNode xmlNode in xmlNodeList) { objectDataList.Add(XmlNodeToObject(itemType, xmlNode, propertySetDict, propertyNameDict, reflectionType)); } if (propertySetDict != null && propertySetDict.ContainsKey(propertyInfo.Name)) { propertySetDict[propertyInfo.Name](t, objectDataList); } else { ReflectionHelper.SetPropertyValue(t, objectDataList, propertyInfo); } }
/// <summary> /// 返回实体数据列表 /// </summary> /// <typeparam name="T">泛型实体对象</typeparam> /// <param name="xmlDocument">XmlDocument</param> /// <param name="xPath">XPath 查询语句</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static List <T> ToEntityList <T>(XmlDocument xmlDocument, string xPath, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); Type type = typeof(T); Dictionary <PropertyInfo, XmlTAttribute> propertyNameDict = InitXmlToEntityMapper(type); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } // 根据 XPath 查询 XmlNodeList 数据 XmlNodeList xmlNodeList = xmlDocument.SelectNodes(xPath); foreach (XmlNode xmlNode in xmlNodeList) { dataList.Add(XmlNodeToEntity <T>(xmlNode, propertySetDict, propertyNameDict, reflectionType)); } return(dataList); }
internal static T ExecuteGetCookie <T>(HttpRequestBase httpRequest, string cookieName, Dictionary <string, object> propertyDict, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { HttpCookie httpCookie = GetHttpCookie(httpRequest, cookieName); if (httpCookie == null) { return(null); } Dictionary <PropertyInfo, string> mapperDict = CommonHelper.InitPropertyReadMapper <T, CookieTAttribute>(propertyDict, (name) => httpCookie.Values[name] != null); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } T t = ReflectionGenericHelper.New <T>(); foreach (var keyValueItem in mapperDict) { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, HttpUtility.UrlDecode(httpCookie[keyValueItem.Value]), keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, HttpUtility.UrlDecode(httpCookie[keyValueItem.Value]), keyValueItem.Key); } } return(t); }
private static List <T> ToEntityListNormal <T>(HtmlDocument htmlDocument, string xPath, List <XPathMatch> xPathMatchList = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { HtmlNode documentNode = htmlDocument.DocumentNode; if (documentNode == null) { return(null); } List <T> dataList = new List <T>(); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } Dictionary <PropertyInfo, HtmlAttributeMapperItem> attributeMapperDict = GetAttributeMapperDict <T>(propertyMatchList); HtmlNodeCollection htmlNodeCollection = documentNode.SelectNodes(xPath); if (htmlNodeCollection == null || htmlNodeCollection.Count == 0) { return(null); } Dictionary <PropertyInfo, XPathMatch> propertyInfoXPathDict = new Dictionary <PropertyInfo, XPathMatch>(); XPathMatch matchItem = null; foreach (HtmlNode htmlNode in htmlNodeCollection) { T t = new T(); ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) => { matchItem = null; if (propertyInfoXPathDict.ContainsKey(propertyInfo)) { matchItem = propertyInfoXPathDict[propertyInfo]; } else { matchItem = xPathMatchList.Where(p => p.PropertyName == propertyInfo.Name).FirstOrDefault(); if (matchItem != null) { propertyInfoXPathDict.Add(propertyInfo, matchItem); } } if (matchItem != null) { HtmlNode childHtmlNode = htmlNode.SelectSingleNode(matchItem.XPath); SetEntityValue <T>(t, childHtmlNode, propertyInfo, attributeMapperDict[propertyInfo], propertySetDict); } }); dataList.Add(t); } return(dataList); }
/// <summary> /// 查询数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="queryFieldList">查询字段列表</param> /// <param name="search">搜索内容</param> /// <param name="filter">Filter</param> /// <param name="sortField">排序字段,例:"id desc"</param> /// <param name="pageIndex">页索引</param> /// <param name="pageSize">页大小</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static List <T> Query <T>(string[] queryFieldList, string search, Filter filter, string sortField, int pageIndex, int pageSize, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { IndexSearcher indexSearcher = null; try { List <T> dataList = new List <T>(); BooleanQuery booleanQuery = new BooleanQuery(); if (!string.IsNullOrEmpty(search)) { QueryParser queryParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, queryFieldList, new PanGuAnalyzer()); Query query = queryParser.Parse(search); booleanQuery.Add(query, Lucene.Net.Search.BooleanClause.Occur.MUST); } indexSearcher = new IndexSearcher(IndexDictDirectory, true); bool desc = false; string[] sortFieldList = sortField.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (sortFieldList != null && sortFieldList.Length == 2 && sortFieldList[1] == DESC) { desc = true; } Sort sort = new Sort(new SortField(sortField, SortField.DOC, desc)); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } TopDocs topDocs = indexSearcher.Search(booleanQuery, filter, pageIndex * pageSize, sort); if (topDocs != null && topDocs.totalHits > 0) { Document document = null; for (int i = 0; i < topDocs.totalHits; i++) { document = indexSearcher.Doc(topDocs.scoreDocs[i].doc); dataList.Add(DocumentToEntity <T>(document, propertySetDict)); } } indexSearcher.Close(); return(dataList); } catch { throw; } finally { if (indexSearcher != null) { indexSearcher.Close(); } } }
/// <summary> /// 复制数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="originalData">原实体数据</param> /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param> /// <param name="propertyIgnore">忽略属性列表,例:new { ID = "" }</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static T Copy <T>(object originalData, object propertyMatchList = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), typeof(T), propertyMatchList, propertyIgnore); dynamic propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalData.GetType(), reflectionType); dynamic propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); return(CopyData(typeof(T), propertySetDict, propertyGetDict, propertyMatchDict, originalData, propertyMatchList, propertyIgnore, reflectionType) as T); }
/// <summary> /// 复制数据 /// </summary> /// <param name="originalData">原实体数据</param> /// <param name="targetData">目标实体数据</param> /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param> /// <param name="propertyIgnore">忽略属性列表,例:new { ID = "" }</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static dynamic Copy(object originalData, object targetData, object propertyMatchList = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), targetData.GetType(), propertyMatchList, propertyIgnore); dynamic propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalData.GetType(), reflectionType); dynamic propertySetDict = ReflectionExtendHelper.PropertySetCallDict(targetData.GetType(), reflectionType); object copyData = CopyData(targetData, propertySetDict, propertyGetDict, propertyMatchDict, originalData, propertyMatchList, propertyIgnore, reflectionType); return(Convert.ChangeType(copyData, targetData.GetType())); }
internal static List <T> SheetEntityList <T>(Workbook workbook, string sheetName, Dictionary <string, string> propertyDict = null, int headerIndex = 0, int dataIndex = 1, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); Worksheet sheet = workbook.Worksheets[sheetName]; Cells cellList = sheet.Cells; Dictionary <int, TExcelToEntityColumnMapper> excelToEntityMapperList = InitExcelToEntityMapper <T>(headerIndex, cellList, propertyDict); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } int rowCount = cellList.MaxDataRow + 1; int cellCount = cellList.MaxColumn + 1; for (int index = dataIndex; index < rowCount; index++) { T t = new T(); for (int cellIndex = 0; cellIndex < cellCount; cellIndex++) { if (excelToEntityMapperList.ContainsKey(cellIndex)) { TExcelToEntityColumnMapper columnMapper = excelToEntityMapperList[cellIndex]; Cell cell = cellList[index, cellIndex]; if (cell != null && cell.Value != null) { if (propertySetDict != null && propertySetDict.ContainsKey(columnMapper.ColumnPropertyName)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[columnMapper.ColumnPropertyName], t, cell.StringValue, columnMapper.ColumnPropertyInfo); } else { ReflectionHelper.SetPropertyValue(t, cell.StringValue, columnMapper.ColumnPropertyInfo); } } } } dataList.Add(t); } return(dataList); }
internal static List <T> SheetEntityList <T>(IWorkbook workbook, string sheetName, Dictionary <string, object> propertyDict = null, int headerIndex = 0, int dataIndex = 1, string primaryKey = "", ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); ISheet sheet = workbook.GetSheet(sheetName); if (sheet == null) { throw new Exception(string.Format(ExcelSheetNullException, sheetName)); } bool isHeader = headerIndex != dataIndex; IRow headerRow = sheet.GetRow(headerIndex); int primaryIndex = 0; Dictionary <PropertyInfo, ExcelToEntityColumnMapper> excelToEntityMapperList = InitExcelToEntityMapper <T>(headerRow, isHeader, primaryKey, ref primaryIndex, propertyDict); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } List <ExcelMergeCell> mergeCellList = SheetMergeCellList(sheet); IRow row = null; ICell iCell = null; for (int index = dataIndex; index <= sheet.LastRowNum; index++) { row = sheet.GetRow(index); if (row != null) { iCell = row.GetCell(primaryIndex); // 检查主键列是否有数据 if (iCell != null && !string.IsNullOrEmpty(iCell.ToString())) { dataList.Add(IRowToEntity <T>(index, row, propertySetDict, excelToEntityMapperList, mergeCellList)); } } } return(dataList); }
internal static List <T> ExecuteToEntityList <T>(DataTable dataTable, Dictionary <string, object> propertyDict, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Dictionary <PropertyInfo, string> columnNameDict = CommonHelper.InitPropertyReadMapper <T, DataTableTAttribute>(propertyDict, (name) => dataTable.Columns.Contains(name)); List <T> dataList = new List <T>(); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } // 读取 DataTable 数据到实体对象列表中 foreach (DataRow dataRow in dataTable.Rows) { dataList.Add(DataRowToEntity <T>(dataRow, propertySetDict, columnNameDict)); } return(dataList); }
private static List <T> ToEntityListStrict <T>(HtmlDocument htmlDocument, string xPath, List <XPathMatch> xPathMatchList = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { HtmlNode documentNode = htmlDocument.DocumentNode; if (documentNode == null) { return(null); } List <T> dataList = new List <T>(); HtmlNodeCollection htmlNodeCollection = documentNode.SelectNodes(xPath); if (htmlNodeCollection == null || htmlNodeCollection.Count == 0) { return(null); } Dictionary <string, List <HtmlNode> > propertyNodeDict = GetXPathNodeDict(documentNode, xPathMatchList, htmlNodeCollection.Count); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } Dictionary <PropertyInfo, HtmlAttributeMapperItem> attributeMapperDict = GetAttributeMapperDict <T>(propertyMatchList); int index = 0; foreach (HtmlNode mathHtmlNode in htmlNodeCollection) { dataList.Add(HtmlNodeToEntity <T>(mathHtmlNode, index, propertyNodeDict, propertySetDict, attributeMapperDict)); index++; } return(dataList); }
private static List <T> DataReaderToEntityList <T>(DbCommand command, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); bool initStatus = false; List <string> columnNameList = null; Dictionary <PropertyInfo, string> columnNameDict = null; using (DbDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection)) { dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } if (dataReader.Read()) { if (!initStatus) { columnNameList = new List <string>(); for (int index = 0; index < dataReader.FieldCount; index++) { columnNameList.Add(dataReader.GetName(index)); } columnNameDict = InitDbToEntityMapper <T>(dataReader, propertyMatchList); initStatus = true; } dataList.Add(DataReaderToEntity <T>(dataReader, propertySetDict, columnNameList, columnNameDict)); } while (dataReader.Read()) { dataList.Add(DataReaderToEntity <T>(dataReader, propertySetDict, columnNameList, columnNameDict)); } } return(dataList); }
/// <summary> /// 返回实体数据列表 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="txtPath">Txt 路径</param> /// <param name="splitChar">列分隔符,例:|</param> /// <param name="encoding">编码格式</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="typeEnum">TxtTypeEnum 枚举类型</param> /// <param name="lineSplitChar">行分隔符,例:\r\n</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static List <T> ToEntityList <T>(string txtPath, string splitChar, System.Text.Encoding encoding, object propertyMatchList = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); ExecuteStreamReader(txtPath, encoding, (StreamReader streamReader) => { Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); Dictionary <PropertyInfo, int> propertyNameDict = InitTxtToEntityMapper <T>(propertyDict, typeEnum); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } string content = streamReader.ReadToEnd(); string[] contentList = content.Split(new string[] { lineSplitChar }, StringSplitOptions.None); foreach (string contentItem in contentList) { dataList.Add(TextToEntity <T>(contentItem, splitChar, propertySetDict, propertyNameDict)); } }); return(dataList); }
private static object CopyData(object targetData, dynamic propertySetDict, dynamic propertyGetDict, Dictionary <PropertyInfo, string> propertyMatchDict, dynamic originalData, object propertyMatch = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { if (originalData == null) { return(targetData); } PropertyInfo propertyInfo = null; dynamic propertyValue = null; foreach (var keyValueItem in propertyMatchDict) { propertyInfo = keyValueItem.Key; if (propertyGetDict != null && propertyGetDict.ContainsKey(keyValueItem.Value)) { propertyValue = propertyGetDict[keyValueItem.Value](originalData); } else { propertyValue = ReflectionHelper.GetPropertyValue(originalData, keyValueItem.Value); } if (propertyValue != null) { if (ReflectionHelper.IsListType(propertyInfo.PropertyType)) { Type originalTType = ReflectionHelper.GetListGenericType(originalData.GetType().GetProperty(keyValueItem.Value)); var originalPropertyMatchValue = ReflectionHelper.GetPropertyValue(propertyMatch, propertyInfo.Name); var originalPropertyIgnoreValue = ReflectionHelper.GetPropertyValue(propertyIgnore, propertyInfo.Name); var originalPropertyValue = ReflectionHelper.GetPropertyValue(targetData, propertyInfo) as IList; Type listTType = ReflectionHelper.GetListGenericType(propertyInfo); var objectDataList = ReflectionHelper.NewList(listTType) as IList; dynamic listPropertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { listPropertySetDict = ReflectionExtendHelper.PropertySetCallDict(listTType, reflectionType); } dynamic listPropertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { listPropertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalTType, reflectionType); } for (int index = 0; index < propertyValue.Count; index++) { if (originalPropertyValue != null && index < originalPropertyValue.Count) { objectDataList.Add(CopyData(originalPropertyValue[index], listPropertySetDict, listPropertyGetDict, InitPropertyMatchMapper(originalTType, listTType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue[index], originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType)); } else { objectDataList.Add(CopyData(listTType, listPropertySetDict, listPropertyGetDict, InitPropertyMatchMapper(originalTType, listTType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue[index], originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType)); } } targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, objectDataList, null); } else if (ReflectionHelper.IsCustomType(propertyInfo.PropertyType)) { var originalPropertyMatchValue = ReflectionHelper.GetPropertyValue(propertyMatch, propertyInfo.Name); var originalPropertyIgnoreValue = ReflectionHelper.GetPropertyValue(propertyIgnore, propertyInfo.Name); var originalPropertyValue = ReflectionHelper.GetPropertyValue(targetData, propertyInfo); if (originalPropertyValue == null) { targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, CopyData(propertyInfo.PropertyType, null, null, InitPropertyMatchMapper(propertyValue.GetType(), propertyInfo.PropertyType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue, originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType), null); } else { targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, CopyData(originalPropertyValue, null, null, InitPropertyMatchMapper(propertyValue.GetType(), propertyInfo.PropertyType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue, originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType), null); } } else { if (propertySetDict != null && propertySetDict.ContainsKey(propertyInfo.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[propertyInfo.Name], targetData, propertyValue.ToString(), propertyInfo); } else { ReflectionHelper.SetPropertyValue(targetData, propertyValue.ToString(), propertyInfo); } } } } return(targetData); }
private static object XmlNodeToObject(Type type, XmlNode xmlNode, dynamic propertySetDict, Dictionary <PropertyInfo, XmlTAttribute> propertyNameDict, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { object t = ReflectionHelper.New(type); if (propertyNameDict == null) { propertyNameDict = InitXmlToEntityMapper(type); } if (propertySetDict == null) { if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict(type, reflectionType); } } foreach (var keyValueItem in propertyNameDict) { XmlNodeList childXmlNodeList = null; string propertyValue = null; switch (keyValueItem.Value.NameType) { case XmlTEnum.Attribute: // 如果是获得 XmlNode 属性值 if (xmlNode.Attributes[keyValueItem.Value.Name] != null) { propertyValue = xmlNode.Attributes[keyValueItem.Value.Name].Value; } ; break; case XmlTEnum.Text: // 如果是获得 XmlNode 的 Text 数据 propertyValue = xmlNode.InnerText; break; case XmlTEnum.Xml: // 如果是获得 XmlNode 的子元素数据 propertyValue = xmlNode.InnerXml; break; case XmlTEnum.Element: case XmlTEnum.ElementList: // 如果是元素或者元素列表 childXmlNodeList = xmlNode.SelectNodes("descendant::" + keyValueItem.Value.Name); break; } if (keyValueItem.Value.NameType == XmlTEnum.Element) { SetPropertyValueByElement(t, keyValueItem.Key, childXmlNodeList, propertySetDict, reflectionType); } else if (keyValueItem.Value.NameType == XmlTEnum.ElementList) { SetPropertyValueByElementList(t, keyValueItem.Key, childXmlNodeList, reflectionType); } else { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, propertyValue, keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, propertyValue, keyValueItem.Key); } } } return(t); }