最近做了一个.Net Core环境下,基于NPOI的Excel导入导出以及Word操作的服务封装,
涉及到大量反射操作,在性能优化过程中使用到了表达式树,记录一下。

Excel导入是相对比较麻烦的一块,实现的效果是:调用方只需要定义一个类,只需要标记特性,
服务读取Excel=>校验(正则、必填、整数范围、日期、数据库是否存在、数据重复) =>将校验结果返回 => 提供方法将Excel数据
转换为指定类集合。

在最后一步转换,最开始用反射实现,性能较差;后来通过了反射+委托,表达式树方式进行优化,
最终性能接近了硬编码。见图,转换近5000条有效数据,耗时仅100毫秒不到,是反射的近20倍。

读取Excel数据之后,我将数据读取到了自定义的两个类(方便后面的校验)

   public class ExcelDataRow
{
/// <summary>
/// 行号
/// </summary>
public int RowIndex { get; set; } /// <summary>
/// 单元格数据
/// </summary>
public List<ExcelDataCol> DataCols { get; set; } = new List<ExcelDataCol>(); /// <summary>
/// 是否有效
/// </summary>
public bool IsValid { get; set; } /// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg { get; set; }
} public class ExcelDataCol : ExcelCol
{
/// <summary>
/// 对应属性名称
/// </summary>
public string PropertyName { get; set; } /// <summary>
/// 行号
/// </summary>
public int RowIndex { get; set; } /// <summary>
/// 字符串值
/// </summary>
public string ColValue { get; set; }
}

校验完之后,需要将ExcelDataRow转换为指定类型

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Ade.OfficeService.Excel
{
/// <summary>
/// 生成表达式目录树 缓存
/// </summary>
public class ExpressionMapper
{
private static Hashtable Table = Hashtable.Synchronized(new Hashtable(1024)); /// <summary>
/// 将ExcelDataRow快速转换为指定类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataRow"></param>
/// <returns></returns>
public static T FastConvert<T>(ExcelDataRow dataRow)
{
//利用表达式树,动态生成委托并缓存,得到接近于硬编码的性能
//最终生成的代码近似于(假设T为Person类)
//Func<ExcelDataRow,Person>
// new Person(){
// Name = Convert(ChangeType(dataRow.DataCols.SingleOrDefault(c=>c.PropertyName == prop.Name).ColValue,prop.PropertyType),prop.ProertyType),
// Age = Convert(ChangeType(dataRow.DataCols.SingleOrDefault(c=>c.PropertyName == prop.Name).ColValue,prop.PropertyType),prop.ProertyType)
// }
// } string propertyNames = string.Empty;
dataRow.DataCols.ForEach(c => propertyNames += c.PropertyName + "_");
var key = typeof(T).FullName + "_" + propertyNames.Trim('_'); if (!Table.ContainsKey(key))
{
List<MemberBinding> memberBindingList = new List<MemberBinding>(); MethodInfo singleOrDefaultMethod = typeof(Enumerable)
.GetMethods()
.Single(m => m.Name == "SingleOrDefault" && m.GetParameters().Count() == 2)
.MakeGenericMethod(new[] { typeof(ExcelDataCol) }); foreach (var prop in typeof(T).GetProperties())
{
Expression<Func<ExcelDataCol, bool>> lambdaExpr = c => c.PropertyName == prop.Name; MethodInfo changeTypeMethod = typeof(ExpressionMapper).GetMethods().Where(m => m.Name == "ChangeType").First(); Expression expr =
Expression.Convert(
Expression.Call(changeTypeMethod
, Expression.Property(
Expression.Call(
singleOrDefaultMethod
, Expression.Constant(dataRow.DataCols)
, lambdaExpr)
, typeof(ExcelDataCol), "ColValue"), Expression.Constant(prop.PropertyType))
, prop.PropertyType); memberBindingList.Add(Expression.Bind(prop, expr));
} MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(T)), memberBindingList.ToArray());
Expression<Func<ExcelDataRow, T>> lambda = Expression.Lambda<Func<ExcelDataRow, T>>(memberInitExpression, new ParameterExpression[]
{
Expression.Parameter(typeof(ExcelDataRow), "p")
}); Func<ExcelDataRow, T> func = lambda.Compile();//拼装是一次性的
Table[key] = func;
}
var ss = (Func<ExcelDataRow, T>)Table[key]; return ((Func<ExcelDataRow, T>)Table[key]).Invoke(dataRow);
} public static object ChangeType(string stringValue, Type type)
{
object obj = null; Type nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null)
{
if (stringValue == null)
{
obj = null;
} }
else if (typeof(System.Enum).IsAssignableFrom(type))
{
obj = Enum.Parse(type, stringValue);
}
else
{
obj = Convert.ChangeType(stringValue, type);
} return obj;
}
}
}

  

利用表达式树Expression优化反射性能的更多相关文章

  1. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  2. 表达式树 Expression

    转载泛型方法动态生成表达式树 Expression public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> ...

  3. 表达式树(Expression Tree)

    饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...

  4. C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  5. 【C#表达式树 七】 反射在表达式树中的应用 ListInitExpression

    以下都是反射在表达式树中的应用 对象初始化 Expression.MemberInit 反射获取成员(字段 或者属性),绑定数据,然后生成 成员表达式节点 class Animal { public ...

  6. 泛型方法动态生成表达式树 Expression

    public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...

  7. jQuery find() 搜索所有段落中的后代 C# find() 第一个匹配元素 Func 有返回值 Action是没有返回值 Predicate 只有一个参数且返回值为bool 表达式树Expression

    所有p后代span Id为 TotalProject 的 select 标签 的后代 option标签 为选中的 text using System; using System.Collections ...

  8. 表达式树Expression

    Expression表达式树动态查询 在进行数据列表的查询中,我们通常会使用两种方式进行查询: linq查询 数据库sql语句查询 这样固然可以实现查询,本人之前也都是这么做的,因为查询的条件很少.使 ...

  9. C# 表达式树 Expression

    表达式树是定义代码的数据结构. 它们基于编译器用于分析代码和生成已编译输出的相同结构. 几种常见的表达式 BinaryExpression 包含二元运算符的表达式 BinaryExpression b ...

随机推荐

  1. 编码,charset,乱码,unicode,utf-8与net简单释义

    1.文件分为文本文件和二进制文件﹐不过本质都一样﹐都是些01. 2.计算机存储设备存储的0或1﹐称为计算机的一个二进制位(bit). 3.二进制文件的0和1有专门的应用程序来读﹐所以它们没有什么乱不乱 ...

  2. MapReduce分区的使用(Partition)

    MapReduce中的分区默认是哈希分区,根据map输出key的哈希值做模运算,如下 int result = key.hashCode()%numReduceTask; 如果我们需要根据业务需求来将 ...

  3. ffmpeg 获取视频宽高

    int main(int argc, char *argv[]) { const char* file_name = "video.mp4"; int ret; unsigned ...

  4. TF卡

    1.我的手机 64G 2.冬冬手机 16G 3.茜茜收音机 8G(创见) 4.父 手机 8G(Kingston) 5. 6. 7.

  5. intel dpdk api interrupt module 中断模块介绍

    声明:此文档只做学习交流使用,请勿用作其他商业用途 author:朝阳_tonyE-mail : linzhaolover@gmail.comCreate Date: 2013-7-12 11:46: ...

  6. 在eclipse中引入jquery.js文件报错的解决方案

    从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在myeclipse中报错:jquery.js -> 鼠标右键 -> MyE ...

  7. css 网站素装 追忆过去

    素装代码,以表哀悼等.以下为全站CSS代码. html { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: ...

  8. OPcache

    1.介绍 OPcache 通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销 2.配置 2.1 opcac ...

  9. hihocoder-1285 智力竞赛(区间dp)

    智力竞赛 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队参加一个智力竞赛.竞赛采用过关制,共计N个关卡.在第i个关卡中,小 ...

  10. jquery中ON方法的使用

    以前在jquery中绑定动态元素一直使用live,现在才发现live已经被抛弃了,现在如果想实现live方法,可以使用最新的ON方法,具体使用如下: 替换live() live()写法   $('#l ...