最近做了一个.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. MacacaUIFinder元素查找器

    MacacaUIFinder元素查找器 1. 这是什么? 这是一个用JavaSwing写的Macaca元素查找器,方便测试的小伙伴们定位元素,如果觉得不错,还请点个Star鼓励下,嘿嘿. 地址:htt ...

  2. QT 操作数据库SQLite实例

    #include "widget.h" #include <QApplication> #include <QtSql> #include <QTex ...

  3. mysql 启动服务错误

    在博客上看到下面这个文档解决了问题.推荐可以看下. http://blog.csdn.net/yaowuliu/article/details/51133279

  4. jquery实现自定义弹出框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Angular-ui-router路由,View管理

    ui-router的工作原理非常类似于Angular的路由控制,他只关注状态. Angular模板 最简单的模板,例如main.html: <body data-ng-app="myA ...

  6. 英语发音规则---/ŋ/与/ŋg/的读音区别

    英语发音规则---/ŋ/与/ŋg/的读音区别 一.总结 一句话总结: 1.位于词中间的字母组合ng,有时读作/ ŋ /,有时读作/ ŋg/? singer ['sɪŋə] n. 歌手 ringing ...

  7. linux-常用指令3

    系统管理命令 stat              显示指定文件的详细信息,比ls更详细 who               显示在线登陆用户 whoami          显示当前操作用户 host ...

  8. linux命令学习笔记(6):rmdir 命令

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录 被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.) ...

  9. 【遍历二叉树】11把二叉树转换成前序遍历的链表【Flatten Binary Tree to Linked List】

    本质上是二叉树的root->right->left遍历. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  10. 【LeetCode】060. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...