示例#1
0
        private static Dictionary <PropertyInfo, int> InitTxtToEntityMapper <T>(Dictionary <string, object> propertyMatch = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal) where T : class
        {
            int propertyIndex = 0;
            Dictionary <PropertyInfo, int> propertyDict = new Dictionary <PropertyInfo, int>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (propertyMatch != null && propertyMatch.ContainsKey(propertyInfo.Name))
                {
                    propertyDict.Add(propertyInfo, int.Parse(propertyMatch[propertyInfo.Name].ToString()));
                }
                else
                {
                    if (typeEnum == TxtTypeEnum.Attribute)
                    {
                        TxtTAttribute attribute = propertyInfo.GetCustomAttribute <TxtTAttribute>();
                        if (attribute != null && (attribute.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || attribute.Type == AttributeReadAndWriteTypeEnum.Read))
                        {
                            propertyDict.Add(propertyInfo, attribute.Index);
                        }
                    }
                    else
                    {
                        propertyDict.Add(propertyInfo, propertyIndex);
                        propertyIndex++;
                    }
                }
            });
            return(propertyDict);
        }
示例#2
0
        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);
        }
 /// <summary>
 /// 获取类属性的设置值委托列表
 /// </summary>
 /// <param name="type">Type</param>
 /// <param name="reflectionType">反射类型</param>
 /// <returns></returns>
 public static dynamic PropertySetCallDict(Type type, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
 {
     return(ExecutePropertySetCall(type, () =>
     {
         return ReflectionGenericHelper.PropertySetCallDict(type, reflectionType);
     }, reflectionType));
 }
 /// <summary>
 /// 获取类属性的设置值委托
 /// </summary>
 /// <param name="type">Type</param>
 /// <param name="propertyName">属性名称</param>
 /// <param name="reflectionType">反射类型</param>
 /// <returns></returns>
 public static dynamic PropertySetCall(Type type, string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
 {
     return(ExecutePropertySetCall(type, propertyName, () =>
     {
         return ReflectionGenericHelper.PropertySetCall(type, type.GetProperty(propertyName), reflectionType);
     }, reflectionType));
 }
 /// <summary>
 /// 获取类属性的获取值委托
 /// </summary>
 /// <param name="type">Type</param>
 /// <param name="propertyInfo">PropertyInfo</param>
 /// <param name="reflectionType">反射类型</param>
 /// <returns></returns>
 public static dynamic PropertyGetCall(Type type, PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
 {
     return(ExecutePropertyGetCall(type, propertyInfo.Name, () =>
     {
         return ReflectionGenericHelper.PropertyGetCall(type, propertyInfo, reflectionType);
     }, reflectionType));
 }
示例#7
0
        private static Dictionary <PropertyInfo, HtmlAttributeMapperItem> GetAttributeMapperDict <T>(object propertyMatchList) where T : class
        {
            Dictionary <string, object> propertyMatchDict = CommonHelper.GetParameterDict(propertyMatchList);
            Dictionary <PropertyInfo, HtmlAttributeMapperItem> propertyDict = new Dictionary <PropertyInfo, HtmlAttributeMapperItem>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                HtmlAttributeMapperItem mapperItem = new HtmlAttributeMapperItem();

                if (propertyMatchDict != null && propertyMatchDict.ContainsKey(propertyInfo.Name))
                {
                    mapperItem.AttributeName = propertyMatchDict[propertyInfo.Name].ToString();
                    mapperItem.AttributeEnum = HtmlAnalysisAttributeEnum.Attribute;
                }
                else
                {
                    HtmlAnalysisTAttribute attribute = propertyInfo.GetCustomAttribute <HtmlAnalysisTAttribute>();
                    if (attribute != null)
                    {
                        mapperItem.AttributeName = attribute.Name;
                        mapperItem.AttributeEnum = attribute.NameType;
                    }
                    else
                    {
                        mapperItem.AttributeName = propertyInfo.Name;
                        mapperItem.AttributeEnum = HtmlAnalysisAttributeEnum.Attribute;
                    }
                }
                propertyDict.Add(propertyInfo, mapperItem);
            });
            return(propertyDict);
        }
示例#8
0
        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 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 List <LuceneIndexModel> EntityToLuceneIndexModel <T>(T t, dynamic propertyGetDict, List <string> propertyList) where T : class
        {
            List <LuceneIndexModel> modelList = new List <LuceneIndexModel>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (propertyList == null || propertyList.IndexOf(propertyInfo.Name) < 0)
                {
                    object value = null;
                    if (propertyGetDict != null && propertyGetDict.ContainsKey(propertyInfo.Name))
                    {
                        value = propertyGetDict[propertyInfo.Name](t);
                    }
                    else
                    {
                        value = ReflectionHelper.GetPropertyValue(t, propertyInfo);
                    }
                    LuceneIndexTAttribute attribute = propertyInfo.GetCustomAttribute <LuceneIndexTAttribute>();
                    if (attribute != null)
                    {
                        LuceneIndexModel model = new LuceneIndexModel()
                        {
                            Name      = propertyInfo.Name,
                            Value     = value != null ? value.ToString() : "",
                            StoreEnum = GetIndexStoreEnum(attribute.StoreEnum),
                            IndexEnum = GetIndexIndexEnum(attribute.IndexEnum)
                        };
                        modelList.Add(model);
                    }
                }
            });

            return(modelList);
        }
示例#11
0
        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="reflectionType">反射类型</param>
        /// <returns></returns>
        public static dynamic PropertySetCallDict <T>(ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
        {
            Type type = typeof(T);

            return(ExecutePropertySetCall(type, () =>
            {
                return ReflectionGenericHelper.PropertySetCallDict <T>(reflectionType);
            }, reflectionType));
        }
        /// <summary>
        /// 获取类属性的设置值委托
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="propertyName">属性名称</param>
        /// <param name="reflectionType">反射类型</param>
        /// <returns></returns>
        public static dynamic PropertySetCall <T>(string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
        {
            Type type = typeof(T);

            return(ExecutePropertySetCall(type, propertyName, () =>
            {
                return ReflectionGenericHelper.PropertySetCall <T>(type.GetProperty(propertyName), reflectionType);
            }, reflectionType));
        }
        /// <summary>
        /// 获取类属性的获取值委托
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="propertyInfo">PropertyInfo</param>
        /// <param name="reflectionType">反射类型</param>
        /// <returns></returns>
        public static dynamic PropertyGetCall <T>(PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
        {
            Type type = typeof(T);

            return(ExecutePropertyGetCall(type, propertyInfo.Name, () =>
            {
                return ReflectionGenericHelper.PropertyGetCall <T>(propertyInfo, reflectionType);
            }, reflectionType));
        }
示例#15
0
 public static List <string> GetFilterNameList <T>(List <string> filterNameList, bool filterPropertyContain) where T : class
 {
     if (filterNameList == null)
     {
         filterNameList = ReflectionGenericHelper.GetPropertyNameList <T>();
     }
     else
     {
         if (!filterPropertyContain)
         {
             filterNameList = ReflectionGenericHelper.GetPropertyNameList <T>().Except <string>(filterNameList).ToList();
         }
     }
     return(filterNameList);
 }
示例#16
0
        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);
                }
            }
        }
示例#17
0
        /// <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 Dictionary <string, EntityToXmlMapper> InitEntityToXmlMapper <T>(T t, dynamic propertyGetDict, List <string> propertyList) where T : class
        {
            Dictionary <string, EntityToXmlMapper> mapperDict = new Dictionary <string, EntityToXmlMapper>();
            string        propertyValue = null;
            XmlTAttribute attribute     = null;

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (propertyList == null || propertyList.IndexOf(propertyInfo.Name) >= 0)
                {
                    if (propertyGetDict != null && propertyGetDict.ContainsKey(propertyInfo.Name))
                    {
                        propertyValue = propertyGetDict[propertyInfo.Name](t).ToString();
                    }
                    else
                    {
                        propertyValue = ReflectionHelper.GetPropertyValue(t, propertyInfo).ToString();
                    }
                    attribute = propertyInfo.GetCustomAttribute <XmlTAttribute>();
                    if (attribute != null)
                    {
                        if (!string.IsNullOrEmpty(attribute.Name))
                        {
                            mapperDict.Add(attribute.Name, new EntityToXmlMapper()
                            {
                                Value = propertyValue, ValueEnum = attribute.NameType
                            });
                        }
                        else
                        {
                            mapperDict.Add(propertyInfo.Name, new EntityToXmlMapper()
                            {
                                Value = propertyValue, ValueEnum = attribute.NameType
                            });
                        }
                    }
                    else
                    {
                        mapperDict.Add(propertyInfo.Name, new EntityToXmlMapper()
                        {
                            Value = propertyValue, ValueEnum = XmlTEnum.Attribute
                        });
                    }
                }
            });
            return(mapperDict);
        }
        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);
        }
示例#20
0
        private static Dictionary <int, PropertyInfo> InitEntityToTxtMapper <T>(object propertyMatchList = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal, string[] propertyList = null, bool propertyContain = true) where T : class
        {
            List <string> filterNameList = null;

            if (propertyList != null)
            {
                filterNameList = propertyList.ToList <string>();
            }

            int propertyIndex = 0;

            Dictionary <string, object>    propertyMatchDict = CommonHelper.GetParameterDict(propertyMatchList);
            Dictionary <int, PropertyInfo> propertyNameDict  = new Dictionary <int, PropertyInfo>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (filterNameList == null || (propertyContain && filterNameList.IndexOf(propertyInfo.Name) >= 0) || (!propertyContain && filterNameList.IndexOf(propertyInfo.Name) < 0))
                {
                    if (propertyMatchDict != null && propertyMatchDict.ContainsKey(propertyInfo.Name))
                    {
                        propertyNameDict.Add(int.Parse(propertyMatchDict[propertyInfo.Name].ToString()), propertyInfo);
                    }
                    else
                    {
                        if (typeEnum == TxtTypeEnum.Attribute)
                        {
                            TxtTAttribute attribute = propertyInfo.GetCustomAttribute <TxtTAttribute>();
                            if (attribute != null && (attribute.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || attribute.Type == AttributeReadAndWriteTypeEnum.Write))
                            {
                                object attributeIndex = ReflectionExtendHelper.GetAttributeValue <TxtTAttribute>(typeof(T), propertyInfo, p => p.Index);
                                if (attributeIndex != null)
                                {
                                    propertyNameDict.Add(int.Parse(attributeIndex.ToString()), propertyInfo);
                                }
                            }
                        }
                        else
                        {
                            propertyNameDict.Add(propertyIndex, propertyInfo);
                            propertyIndex++;
                        }
                    }
                }
            });
            return(propertyNameDict);
        }
示例#21
0
        private static Dictionary <PropertyInfo, string> InitDbToEntityMapper <T>(DbDataReader reader, object propertyMatchList = null) where T : class
        {
            Dictionary <PropertyInfo, string> resultDict = new Dictionary <PropertyInfo, string>();

            if (propertyMatchList != null)
            {
                Dictionary <string, object> propertyMatchDict = CommonHelper.GetParameterDict(propertyMatchList);
                ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
                {
                    string columnName    = propertyInfo.Name;
                    object propertyValue = propertyMatchDict[propertyInfo.Name];
                    if (propertyValue != null)
                    {
                        columnName = propertyValue.ToString();
                    }
                    resultDict.Add(propertyInfo, columnName);
                });
                return(resultDict);
            }

            string key = typeof(T).FullName;

            if (PropertyAttributeDict.ContainsKey(key))
            {
                return(PropertyAttributeDict[key]);
            }

            lock (lockItem)
            {
                ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
                {
                    string columnName = ReflectionExtendHelper.GetAttributeValue <DataBaseTAttribute>(typeof(T), propertyInfo, p => p.Name);
                    if (string.IsNullOrEmpty(columnName))
                    {
                        columnName = propertyInfo.Name;
                    }
                    resultDict.Add(propertyInfo, columnName);
                });
                if (!PropertyAttributeDict.ContainsKey(key))
                {
                    PropertyAttributeDict.Add(key, resultDict);
                }
                return(resultDict);
            }
        }
        internal static Dictionary <string, PropertyInfo> InitPropertyWriteMapperFormat <T, K>(Dictionary <string, object> propertyDict = null, List <string> filterNameList = null, bool filterPropertyContain = true)
            where T : class
            where K : BaseReadAndWriteTAttribute
        {
            Dictionary <string, PropertyInfo> resultDict = new Dictionary <string, PropertyInfo>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (filterNameList == null || (filterPropertyContain && filterNameList.IndexOf(propertyInfo.Name) >= 0) || (!filterPropertyContain && filterNameList.IndexOf(propertyInfo.Name) < 0))
                {
                    string attributeName = null;
                    if (propertyDict != null && propertyDict.ContainsKey(propertyInfo.Name))
                    {
                        attributeName = propertyDict[propertyInfo.Name].ToString();
                    }
                    else
                    {
                        K k = propertyInfo.GetCustomAttribute <K>();
                        if (k != null)
                        {
                            if (k.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || k.Type == AttributeReadAndWriteTypeEnum.Write)
                            {
                                if (!string.IsNullOrEmpty(k.Name))
                                {
                                    attributeName = k.Name;
                                }
                                else
                                {
                                    attributeName = propertyInfo.Name;
                                }
                            }
                        }
                        else
                        {
                            attributeName = propertyInfo.Name;
                        }
                    }
                    if (!string.IsNullOrEmpty(attributeName))
                    {
                        resultDict.Add(attributeName, propertyInfo);
                    }
                }
            });
            return(resultDict);
        }
示例#23
0
        public static Dictionary <PropertyInfo, string> InitPropertyReadMapper <T, K>(Dictionary <string, object> propertyDict, Func <string, bool> callback)
            where T : class
            where K : BaseReadAndWriteTAttribute
        {
            Dictionary <PropertyInfo, string> resultDict = new Dictionary <PropertyInfo, string>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                string mapperName = null;
                if (propertyDict != null && propertyDict.ContainsKey(propertyInfo.Name))
                {
                    mapperName = propertyDict[propertyInfo.Name].ToString();
                }
                else
                {
                    K attribute = propertyInfo.GetCustomAttribute <K>();
                    if (attribute != null)
                    {
                        if (attribute.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || attribute.Type == AttributeReadAndWriteTypeEnum.Read)
                        {
                            if (!string.IsNullOrEmpty(attribute.Name))
                            {
                                mapperName = attribute.Name;
                            }
                            else
                            {
                                mapperName = propertyInfo.Name;
                            }
                        }
                    }
                    else
                    {
                        mapperName = propertyInfo.Name;
                    }
                }
                if (mapperName != null && callback(mapperName))
                {
                    resultDict.Add(propertyInfo, mapperName);
                }
            });
            return(resultDict);
        }
示例#24
0
        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);
        }
示例#26
0
        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);
        }
示例#27
0
        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);
        }
示例#28
0
        private static Dictionary <PropertyInfo, ExcelToEntityColumnMapper> InitExcelToEntityMapper <T>(IRow row, bool isHeader, string primaryKey, ref int primaryIndex, Dictionary <string, object> propertyDict = null) where T : class
        {
            Dictionary <string, int> columnNameDict  = null;
            Dictionary <int, int>    columnIndexDict = null;

            if (isHeader)
            {
                columnNameDict = InitExcelPrimaryMapperByName(row, primaryKey, ref primaryIndex);
            }
            else
            {
                columnIndexDict = InitExcelPrimaryMapperByIndex(row, primaryKey, ref primaryIndex);
            }

            Dictionary <PropertyInfo, ExcelToEntityColumnMapper> resultList = new Dictionary <PropertyInfo, ExcelToEntityColumnMapper>();

            ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) =>
            {
                if (isHeader)
                {
                    string columnName = null;
                    if (propertyDict != null && propertyDict.ContainsKey(propertyInfo.Name))
                    {
                        columnName = propertyDict[propertyInfo.Name].ToString();
                    }
                    else
                    {
                        ExcelTAttribute attribute = propertyInfo.GetCustomAttribute <ExcelTAttribute>();
                        if (attribute != null)
                        {
                            if (attribute.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || attribute.Type == AttributeReadAndWriteTypeEnum.Read)
                            {
                                if (!string.IsNullOrEmpty(attribute.Name))
                                {
                                    columnName = attribute.Name;
                                }
                                else
                                {
                                    columnName = propertyInfo.Name;
                                }
                            }
                        }
                        else
                        {
                            columnName = propertyInfo.Name;
                        }
                    }
                    if (columnName != null && columnNameDict.ContainsKey(columnName))
                    {
                        resultList.Add(propertyInfo, new ExcelToEntityColumnMapper()
                        {
                            ColumnName = columnName, ColumnIndex = columnNameDict[columnName]
                        });
                    }
                }
                else
                {
                    int columnIndex = -1;
                    if (propertyDict != null && propertyDict.ContainsKey(propertyInfo.Name))
                    {
                        columnIndex = int.Parse(propertyDict[propertyInfo.Name].ToString());
                    }
                    else
                    {
                        ExcelTAttribute attribute = propertyInfo.GetCustomAttribute <ExcelTAttribute>();
                        if (attribute != null)
                        {
                            columnIndex = attribute.Index;
                        }
                    }
                    if (columnIndexDict.ContainsKey(columnIndex))
                    {
                        resultList.Add(propertyInfo, new ExcelToEntityColumnMapper()
                        {
                            ColumnName = null, ColumnIndex = columnIndex
                        });
                    }
                }
            });
            return(resultList);
        }
        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);
        }
        /// <summary>
        /// 返回实体数据列表
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="excelPath">Excel 路径</param>
        /// <param name="sheetName">Sheet 表单名称</param>
        /// <param name="propertyMatchList">属性匹配,Dictionary&lt;string, object&gt; 或 new {}</param>
        /// <param name="headerIndex">表头起始索引,默认值:0,表示第一行是表头数据,与 dataIndex 相同时,表示 Excel 无表头</param>
        /// <param name="dataIndex">数据行起始索引,默认值:1,表示数据从第二行开始</param>
        /// <param name="reflectionType">反射类型</param>
        /// <returns></returns>
        public static List <T> ToEntityList <T>(string excelPath, string sheetName, object propertyMatchList = null, int headerIndex = 0, int dataIndex = 1, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new()
        {
            List <T> dataList = new List <T>();

            ExecuteIWorkbookRead(excelPath, (Workbook workbook) =>
            {
                Dictionary <string, string> propertyDict = CommonHelper.MergeDict(false, CommonHelper.KeyValueTransform(propertyMatchList), CommonHelper.KeyValueTransform(ReflectionGenericHelper.GetPropertyDict <T>()));
                dataList = SheetEntityList <T>(workbook, sheetName, propertyDict, headerIndex, dataIndex, reflectionType);
            });
            return(dataList);
        }