Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        /// <summary>
        /// 实体对象数据写入 Txt
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="txtPath">Txt 路径</param>
        /// <param name="dataList">实体数据列表</param>
        /// <param name="splitChar">列分隔符,例:|</param>
        /// <param name="typeEnum">TxtTypeEnum 枚举类型</param>
        /// <param name="encoding">编码格式</param>
        /// <param name="propertyMatchList">属性匹配,Dictionary&lt;string, object&gt; 或 new {}</param>
        /// <param name="propertyList">属性列表,如果指定,则按指定属性列表生成文本数据</param>
        /// <param name="lineSplitChar">行分隔符,例:\r\n</param>
        /// <param name="reflectionType">反射类型</param>
        /// <returns></returns>
        public static bool ToTxt <T>(string txtPath, List <T> dataList, string splitChar, System.Text.Encoding encoding, TxtTypeEnum typeEnum, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
        {
            bool result = ExecuteStreamWriter(txtPath, encoding, false, () =>
            {
                StringBuilder stringBuilder = new StringBuilder();

                Dictionary <int, PropertyInfo> propertyNameDict = InitEntityToTxtMapper <T>(propertyMatchList, typeEnum, propertyList, propertyContain);
                List <int> propertyIndexList = propertyNameDict.Keys.ToList <int>();

                propertyIndexList.Sort((int x, int y) =>
                {
                    if (x > y)
                    {
                        return(1);
                    }
                    if (x < y)
                    {
                        return(-1);
                    }
                    return(0);
                });

                dynamic propertyGetDict = null;
                if (reflectionType != ReflectionTypeEnum.Original)
                {
                    propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType);
                }

                foreach (T t in dataList)
                {
                    stringBuilder.Append(EntityToText <T>(t, splitChar, propertyGetDict, propertyNameDict, propertyIndexList, lineSplitChar));
                }
                return(stringBuilder.ToString());
            });

            return(result);
        }
Beispiel #4
0
 /// <summary>
 /// 实体对象数据写入 Txt
 /// </summary>
 /// <typeparam name="T">实体类型</typeparam>
 /// <param name="txtPath">Txt 路径</param>
 /// <param name="dataList">实体数据列表</param>
 /// <param name="splitChar">列分隔符,例:|</param>
 /// <param name="typeEnum">TxtTypeEnum 枚举类型</param>
 /// <param name="propertyMatchList">属性匹配,Dictionary&lt;string, object&gt; 或 new {}</param>
 /// <param name="propertyList">属性列表,如果指定,则按指定属性列表生成文本数据</param>
 /// <param name="propertyContain">是否包含,true 属性包含,flase 属性排除</param>
 /// <param name="lineSplitChar">行分隔符,例:\r\n</param>
 /// <param name="reflectionType">反射类型</param>
 /// <returns></returns>
 public static bool ToTxt <T>(string txtPath, List <T> dataList, string splitChar, TxtTypeEnum typeEnum, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
 {
     return(ToTxt <T>(txtPath, dataList, splitChar, System.Text.Encoding.UTF8, typeEnum, propertyMatchList, propertyList, propertyContain, lineSplitChar, reflectionType));
 }
Beispiel #5
0
        /// <summary>
        /// 返回实体数据列表
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="txtPath">Txt 路径</param>
        /// <param name="splitChar">列分隔符,例:|</param>
        /// <param name="encoding">编码格式</param>
        /// <param name="propertyMatchList">属性匹配,Dictionary&lt;string, object&gt; 或 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);
        }
Beispiel #6
0
 /// <summary>
 /// 返回实体数据列表
 /// </summary>
 /// <typeparam name="T">实体类型</typeparam>
 /// <param name="txtPath">Txt 路径</param>
 /// <param name="splitChar">列分隔符,例:|</param>
 /// <param name="propertyMatchList">属性匹配,Dictionary&lt;string, object&gt; 或 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, object propertyMatchList = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new()
 {
     return(ToEntityList <T>(txtPath, splitChar, System.Text.Encoding.UTF8, propertyMatchList, typeEnum, lineSplitChar, reflectionType));
 }