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); }
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 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); }
/// <summary> /// 复制数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <typeparam name="K">拥有索引器的实体类型</typeparam> /// <typeparam name="P">索引器参数类型,例:int</typeparam> /// <typeparam name="R">索引器返回类型,例:string</typeparam> /// <param name="originalData">原实体数据</param> /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static T Copy <T, K, P, R>(K originalData, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() where K : class { T t = ReflectionGenericHelper.New <T>(); Func <K, P, object> indexCall = ReflectionGenericHelper.PropertyIndexGetCall <K, P, R>(reflectionType); Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), typeof(T), propertyMatchList, null); foreach (var keyValueItem in propertyMatchDict) { P indexName = (P)Convert.ChangeType(keyValueItem.Value, typeof(P), CultureInfo.InvariantCulture); ReflectionHelper.SetPropertyValue(t, indexCall(originalData, indexName).ToString(), keyValueItem.Key); } return(t); }
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); }