【C#】从文本获取实体对象
2017-11-24 10:12
239 查看
/// <summary> /// Gets the list from string. /// </summary> /// <typeparam name="T">输出类型</typeparam> /// <param name="str">The string.</param> /// <param name="separator">字符串分隔符</param> /// <returns></returns> public static List<T> GetListFromString<T>(string str, char separator) { try { byte[] array = Encoding.UTF8.GetBytes(str); MemoryStream stream = new MemoryStream(array); StreamReader reader = new StreamReader(stream); string lineStr; List<T> ResultList = new List<T>(); while ((lineStr = reader.ReadLine()) != null) //按行读取文件 { T model = Activator.CreateInstance<T>(); string[] fields = lineStr.Split(new char[] { separator }); //切割字符串 PropertyInfo[] proInfos = model.GetType().GetProperties(); //获取所有属性 for (int i = 0; i < fields.Length; i++) { proInfos[i].SetValue(model,fields[i]); //对每一个属性赋值 } ResultList.Add(model); } return ResultList; } catch (Exception ex) { throw ex; } }
相关文章推荐
- C#获取EF实体对象或自定义属性类的字段名称和值
- C# 动态获取JSON实体对象
- C#获取EF实体对象或自定义属性类的字段名称和值
- C# 如何获取某个类型或类型实例对象的大小
- c#获取异常对象最内层的报错信息
- HttpServletRequest中直接获取实体对象
- C# 仿MVC从Request请求中返回对象,及获取客户机的MAC地址等.
- C#反射实例——获取及设置对象属性,运行对象方法
- 27. 实体对象编号获取
- android greendao 表关联时数据插入后实体对象不能获取实时更新数据
- C#获取属性纯文本
- c# XML数据解析通用工具, 获取 节点 文本内容
- C# 如何获取某个类型或类型实例对象的大小
- C# 获取所有对象的字符串表示一ToString方法
- C#实体对象序列化成Json,并让字段的首字母小写
- C#中通过WMI的Win32_MemoryDevice对象获取内存信息简介
- c#获取Graphics对象
- C#中将DataTable数据转为实体对象
- C#利用反射获取对象属性值
- [C#]Ajax XMLHttp 对象的获取方法