Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
        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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }