IEnumerable<IEnumerable<string>>结构解析通用解决方案(支持指定属性顺序)
一、前言
类似如下字符串
"ID", "NameValue", "CodeValue", "ExchangeTypeValue", 6, "invalid"
"ID2", "NameValue2", "CodeValue2", "ExchangeTypeValue2", 6, "invalid"
.......
有可能是文件中存在的,或者调用其他程序返回的结构化数据,那么该如何解析?当其他场景中,只是返回顺序(属性顺序)变了,类结构还是一样,又如何应对?当有很多类似场景时,是不是该抽象出一个泛型方法来应对该场景?当然,也不仅仅于上述情况,可能返回的结构是确定,只是形式不一样,这个过程这里暂时省略,因为正则表达式完全能够解析出来。要用以下的方法,必须转换成IEnumerable<IEnumerable<string>>结构,IEnumerable<IEnumerable<string>>结构中IEnumerable<string>为一个对象所有的值,总体是多个对象的值集合。本文中用反射写的(关于IL操作的后续文章提供),相关的类图如下:

二、ResultTransfer的具体实现
ResultTransfer主要用于对IEnumerable<IEnumerable<string>>结构的解析,另外还可以指定params string[] propertyNames属性参数列表来确定解析顺序(也即是属性顺序),主要方法如下:
public static IList<T> Parse<T>(IEnumerable<IEnumerable<string>> entityRows, params string[] propertyNames) where T : new()
第一个参数entityRows为对象列表值集合。
第二个参数propertyNames为可选参数,输入该参数后,如果propertyNames中存在相关属性,则按照propertyNames对应的属性顺序进行解析。否则按照提供的T类中属性的DataMemberAttribute来确定属性顺序进行解析。
实现代码非常简洁和简单,方法具体如下所示:
public static IList<T> Parse<T>(IEnumerable<IEnumerable<string>> entityRows, params string[] propertyNames) where T : new()
{
if (entityRows == null || entityRows.Count() == )
{
return new List<T>();
} IList<T> entities = new List<T>();
var members = new DataMemberAttributeCollection(typeof(T), propertyNames); if (members.Count <= )
{
return new List<T>();
} FuncProvider funcProvider = new FuncProvider(); foreach (var propertyValues in entityRows)
{
if (propertyValues == null || propertyValues.Count() == )
{
continue;
} entities.Add(Generate<T>(propertyValues, members, funcProvider));
} return entities;
} private static T Generate<T>(IEnumerable<string> propertyValues, DataMemberAttributeCollection members,
FuncProvider funcProvider) where T : new()
{
T entity = Activator.CreateInstance<T>();
int memberCount = members.Count;
int propertyCount = propertyValues.Count(); if (memberCount == || propertyCount == )
{
return entity;
} int convertCount = Math.Min(memberCount, propertyCount);
DataMemberAttribute currAttribute;
PropertyInfo currPropertyInfo; int propertyValueIndex = ; foreach (string propertyValue in propertyValues)
{
if (propertyValueIndex >= convertCount)
{
break;
} propertyValueIndex++;
currAttribute = members[propertyValueIndex - ];
currPropertyInfo = currAttribute.PropertyInfo; if (propertyValue == null)
{
currPropertyInfo.SetValue(entity, null, null);
continue;
} if (propertyValue.GetType() == currAttribute.PropertyType)
{
currPropertyInfo.SetValue(entity, propertyValue, null);
}
else
{
object result = funcProvider.DynamicInvoke(currAttribute.PropertyType, (propertyValue ?? string.Empty).ToString());
currPropertyInfo.SetValue(entity, result, null);
}
} return entity;
}
三、DataMemberAttributeCollection的具体实现
DataMemberAttributeCollection集合类主要用于设置解析属性的顺序,同样,该类提供二个参数的构造函数用于生成相应的配置信息public DataMemberAttributeCollection(Type type, params string[] propertyNames)。
主要代码如下:
public void GetConfiguration(Type type, params string[] propertyNames)
{
if (type == null || type.GetProperties().Length <= )
{
return;
} if (propertyNames == null || propertyNames.Length == )
{
AddAllDataMemberAttributes(type);
}
else
{
AddDataMemberAttributes(type, propertyNames);
} this._memberAttributes = this._memberAttributes.OrderBy(p => p.Order).ToList();
} private void AddDataMemberAttributes(Type type, string[] propertyNames)
{
IList<PropertyInfo> validPropertyInfos = new List<PropertyInfo>();
PropertyInfo tempPropertyInfo; foreach (string propertyName in propertyNames)
{
if (string.IsNullOrWhiteSpace(propertyName))
{
continue;
} tempPropertyInfo = type.GetProperty(propertyName.Trim()); if (tempPropertyInfo == null)
{
throw new ArgumentException(string.Format(@"Contains Invalid Property Name Arg : {0}.", propertyName.Trim()));
} validPropertyInfos.Add(tempPropertyInfo);
} if (validPropertyInfos.Count() > )
{
foreach (var property in validPropertyInfos)
{
AddAttributes(new DataMemberAttribute(), property);
}
}
} private void AddAllDataMemberAttributes(Type type)
{
DataMemberAttribute attr = null;
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
attr = AttributeUtility.GetCustomAttribute<DataMemberAttribute>(propertyInfo); if (attr == null)
{
continue;
} if (!attr.IsRequire)
{
continue;
} if (this._memberAttributes.Count(p => p.Order == attr.Order) > )
{
throw new ArgumentException(string.Format(@"Contains Same Order {0}.Please Look Up DataMemberAttribute
Of The Type {1}", attr.Order, type.Name));
} AddAttributes(attr, propertyInfo);
}
} private void AddAttributes(DataMemberAttribute attr, PropertyInfo propertyInfo)
{
if (string.IsNullOrWhiteSpace(attr.Name))
{
attr.Name = propertyInfo.Name;
} attr.PropertyName = propertyInfo.Name;
attr.PropertyType = propertyInfo.PropertyType;
attr.PropertyInfo = propertyInfo; this._memberAttributes.Add(attr);
}
该类确保指定Type的类中DataMemberAttribute是否设置正确(是否有相同的Order),确保是否输入了错误的propertyName。
四、具体应用
对于具体应用的话,用单元测试来得方便与直接。
(1)对于只输入一个参数的应用如下:
[TestMethod()]
public void ParseTest()
{
IList<IList<string>> entityRows = new List<IList<string>>();
entityRows.Add(new List<string>() { "", "NameValue", "CodeValue", "ExchangeTypeValue", "", "invalid" }); var contracts = ResultTransfer.Parse<ContinousContract>(entityRows); Assert.IsNotNull(contracts);
Assert.IsTrue(contracts.Count == );
Assert.AreEqual(contracts[].Code, "CodeValue");
Assert.AreEqual(contracts[].Name, "NameValue");
Assert.AreEqual(contracts[].ExchangeType, "ExchangeTypeValue");
Assert.AreEqual(contracts[].OrgidID, );
Assert.AreEqual(contracts[].ExchangeTypeValue, );
}
(2)对于只输入无效参数的应用如下:
[TestMethod()]
public void ParseWithInvalidArgTest()
{
IList<IList<string>> entityRows = new List<IList<string>>();
entityRows.Add(new List<string>() { "sss", "NameValue", "CodeValue", "ExchangeTypeValue", "", "invalid" }); var contracts = ResultTransfer.Parse<ContinousContract>(entityRows); Assert.IsNotNull(contracts);
Assert.IsTrue(contracts.Count == );
Assert.AreEqual(contracts[].Code, "CodeValue");
Assert.AreEqual(contracts[].Name, "NameValue");
Assert.AreEqual(contracts[].ExchangeType, "ExchangeTypeValue");
Assert.AreEqual(contracts[].OrgidID, );
Assert.AreEqual(contracts[].ExchangeTypeValue, );
}
输入无效的IEnumerable<IEnumerable<string>>参数,方法内部会进行隐式的转换,比如“sss”转换成0。
(3)对于二个参数时的应用如下:
[TestMethod()]
public void ParseWithArgTest()
{
IList<IList<string>> entityRows = new List<IList<string>>();
entityRows.Add(new List<string>() { "", "NameValue", "ExchangeTypeValue", "", "invalid" });
var propertyNames = new List<string>() { "ExchangeTypeValue", "Name", "", "ExchangeType" }; var contracts = ResultTransfer.Parse<ContinousContract>(entityRows, propertyNames.ToArray()); Assert.IsNotNull(contracts);
Assert.IsTrue(contracts.Count == );
Assert.AreEqual(contracts[].Code, null);
Assert.AreEqual(contracts[].Name, "NameValue");
Assert.AreEqual(contracts[].ExchangeType, "ExchangeTypeValue");
Assert.AreEqual(contracts[].OrgidID, );
Assert.AreEqual(contracts[].ExchangeTypeValue, );
}
一旦输入二个参数,且propertyNames参数的个数大于0,则以propertyNames对应的属性顺序进行解析。对于输入错误的属性名,方法内部会抛出异常,当然也可以增加一个参数用于控制是否抛出异常,或者写入日志文件中等。
对于将固定格式的字符串解析成IEnumerable<IEnumerable<string>>,正则表达式解析的话比较简单,此文不做讲解,略过...
IEnumerable<IEnumerable<string>>结构解析通用解决方案(支持指定属性顺序)的更多相关文章
- 一位有着工匠精神的博主写的关于IEnumerable接口的详细解析
在此,推荐一位有着工匠精神的博主写的一篇关于IEnumerable接口的深入解析的文章:http://www.cnblogs.com/zhaopei/p/5769782.html#autoid-0-0 ...
- iOS沙盒目录结构解析
iOS沙盒目录结构解析 原文地址:http://blog.csdn.net/wzzvictory/article/details/18269713 出于安全考虑,iOS系统的沙盒机制规定每个应 ...
- 逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X])
常用技能:http://www.cnblogs.com/dunitian/p/4822808.html#skill 逆天博客:http://dnt.dkil.net 逆天通用水印扩展篇~新增剪贴板系列 ...
- 业务安全通用解决方案——WAF数据风控
业务安全通用解决方案——WAF数据风控 作者:南浔@阿里云安全 “你们安全不要阻碍业务发展”.“这个安全策略降低用户体验,影响转化率”——这是甲方企业安全部门经常听到合作团队抱怨.但安全从业者加入公司 ...
- Spring Web MVC 多viewResolver视图解析器解决方案
viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...
- H.264码流结构解析
from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...
- Redis源码剖析--源码结构解析
请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...
- InfluxDB源码目录结构解析
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 influxdata主目录结构 [root@localhost ...
- Atitit 大json文件的结构化查看解决方案,高性能的jsonview attilax总结.docx
Atitit 大json文件的结构化查看解决方案,高性能的jsonview attilax总结.docx 1.1. 实现目标:1 1.2. 实现key与value类型的..一直分析到非 jsonob ...
随机推荐
- JavaWeb技术(一):JDBC简介
一. JDBC简介 1. Java Database Connectivity(JDBC) 使用JDBC可以对数据库进行访问 2. JDBC的核心接口 1)DriverManager 驱动管理器接口 ...
- Mac删除.DS_Store文件
1.删除.DS_Store文件 sudo find ./ -name ".DS_Store" -depth -exec rm {} \; 2.禁止生成此文件 defaults wr ...
- 您还有心跳吗?超时机制分析(java)
注:本人是原作者,首发于并发编程网(您还有心跳吗?超时机制分析),此文结合那里的留言作了一些修改. 问题描述 在C/S模式中,有时我们会长时间保持一个连接,以避免频繁地建立连接,但同时,一般会有一个超 ...
- PHP获取页面执行时间的方法
一些循环代码,有时候要知道页面执行的时间,可以添加以下几行代码到页面头部和尾部: 头部: <?php $stime=microtime(true); 尾部: $etime=microtime(t ...
- 20151208Study
20151208-----------------------------------------------------* Her main interest now is raising her ...
- 强大的Spring缓存技术(下)
基本原理 一句话介绍就是Spring AOP的动态代理技术. 如果读者对Spring AOP不熟悉的话,可以去看看官方文档 扩展性 直到现在,我们已经学会了如何使用开箱即用的 spring cache ...
- 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用
错误提示: 服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用.请点击 Web 浏览器中的“刷新”按钮重试您的请求. 管理员注意事项: 详述此特定请求失败原因的错误信 ...
- Ubuntu下deb包的安装方法 (zz)
Ubuntu下deb包的安装方法 分类: Ubuntu10使用技巧 2010-10-11 23:49 42969人阅读 评论(3) 收藏 举报 ubuntudebdebianlinux deb是deb ...
- Jpanel和container和jframe的区别
Jpanel和container和jframe的区别 (2012-05-23 19:15:11) 转载▼ 标签: 杂谈 分类: room 看到上上面的几张图,container容器是位于最高层. 下面 ...
- hdoj 2039 三角形
Problem Description 给定三条边,请你判断一下能不能组成一个三角形. Input 输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C.其中A,B,C & ...