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 T IRowToEntity <T>(int rowIndex, IRow row, dynamic propertySetDict, Dictionary <PropertyInfo, ExcelToEntityColumnMapper> excelToEntityMapperList, List <ExcelMergeCell> mergeCellList) where T : class, new() { T t = ReflectionGenericHelper.New <T>(); ICell iCell = null; string cellText = null; foreach (var keyValueItem in excelToEntityMapperList) { cellText = GetSheetMergeCellText(mergeCellList, rowIndex, keyValueItem.Value.ColumnIndex); if (cellText == null) { iCell = row.GetCell(keyValueItem.Value.ColumnIndex); if (iCell != null) { cellText = GetCellText(iCell, keyValueItem.Key.PropertyType); } } if (!string.IsNullOrEmpty(cellText)) { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, cellText, keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, cellText, keyValueItem.Key); } } } return(t); }
private static T HtmlNodeToEntity <T>(HtmlNode htmlNode, int index, Dictionary <string, List <HtmlNode> > nodeDict, dynamic propertySetDict, Dictionary <PropertyInfo, HtmlAttributeMapperItem> attributeMapperDict) where T : class, new() { T t = ReflectionGenericHelper.New <T>(); HtmlNode dataNode = htmlNode; foreach (var keyValueItem in attributeMapperDict) { if (nodeDict != null && nodeDict.ContainsKey(keyValueItem.Key.Name)) { dataNode = nodeDict[keyValueItem.Key.Name][index]; } string attributeValue = GetNodeText(dataNode, keyValueItem.Value.AttributeName, keyValueItem.Value.AttributeEnum); if (!string.IsNullOrEmpty(attributeValue)) { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, attributeValue, keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, attributeValue, keyValueItem.Key); } } } return(t); }
private static T DocumentToEntity <T>(Document document, dynamic propertySetDict) where T : class, new() { T t = ReflectionGenericHelper.New <T>(); ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) => { LuceneIndexTAttribute attribute = propertyInfo.GetCustomAttribute <LuceneIndexTAttribute>(); if (attribute != null) { string field = document.Get(propertyInfo.Name); if (!string.IsNullOrEmpty(field)) { if (propertySetDict != null && propertySetDict.ContainsKey(propertyInfo.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[propertyInfo.Name], t, field, propertyInfo); } else { ReflectionHelper.SetPropertyValue(t, field, propertyInfo); } } } }); return(t); }
private static void SetEntityValue <T>(T t, HtmlNode htmlNode, PropertyInfo propertyInfo, HtmlAttributeMapperItem mapperItem, dynamic propertySetDict) where T : class { string attributeValue = GetNodeText(htmlNode, mapperItem.AttributeName, mapperItem.AttributeEnum); if (!string.IsNullOrEmpty(attributeValue)) { if (propertySetDict != null && propertySetDict.ContainsKey(propertyInfo.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[propertyInfo.Name], t, attributeValue, propertyInfo); } else { ReflectionHelper.SetPropertyValue(t, attributeValue, propertyInfo); } } }
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); }
private static T DataReaderToEntity <T>(DbDataReader reader, dynamic propertySetDict, List <string> columnNameList, Dictionary <PropertyInfo, string> columnNameDict) where T : class, new() { T t = ReflectionGenericHelper.New <T>(); foreach (var keyValueItem in columnNameDict) { if (columnNameList.IndexOf(keyValueItem.Value) >= 0) { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, reader[keyValueItem.Value].ToString(), keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, reader[keyValueItem.Value].ToString(), keyValueItem.Key); } } } return(t); }
private static T DataRowToEntity <T>(DataRow dataRow, dynamic propertySetDict, Dictionary <PropertyInfo, string> columnNameDict) where T : class, new() { T t = ReflectionGenericHelper.New <T>(); foreach (var keyValueItem in columnNameDict) { if (dataRow[keyValueItem.Value] != null) { if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, dataRow[keyValueItem.Value].ToString(), keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, dataRow[keyValueItem.Value].ToString(), keyValueItem.Key); } } } return(t); }
private static T TextToEntity <T>(string text, string splitChar, dynamic propertySetDict, Dictionary <PropertyInfo, int> propertyNameDict) where T : class, new() { // 按分隔符拆分文本内容 string[] dataList = text.Split(new string[] { splitChar }, StringSplitOptions.None); T t = ReflectionGenericHelper.New <T>(); foreach (var keyValueItem in propertyNameDict) { if (keyValueItem.Value >= 0 && keyValueItem.Value < dataList.Length) { // 设置实体对象属性数据 if (propertySetDict != null && propertySetDict.ContainsKey(keyValueItem.Key.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[keyValueItem.Key.Name], t, dataList[keyValueItem.Value], keyValueItem.Key); } else { ReflectionHelper.SetPropertyValue(t, dataList[keyValueItem.Value], keyValueItem.Key); } } } return(t); }
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); }