只要十步,你就可以应用表达式树来优化动态调用(二)( 四 )

validateFuncExpression){var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk));Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null");var namePropExp = Expression.Property(inputExp, propertyInfo);var nameNameExp = Expression.Constant(propertyInfo.Name);var requiredMethodExp = Expression.Invoke(validateFuncExpression,nameNameExp,namePropExp);var assignExp = Expression.Assign(resultExp, requiredMethodExp);var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty);var conditionExp = Expression.IsFalse(resultIsOkPropertyExp);var ifThenExp =Expression.IfThen(conditionExp,Expression.Return(returnLabel, resultExp));var re = Expression.Block(new[] {resultExp},assignExp,ifThenExp);return re;}}}catch (Exception e){Console.WriteLine(e);throw;}}[Test]public void Run(){// see code in demo repo}}}代码要点:

  1. CreateValidateExpression 就是被抽离出来的公共方法 。
  2. 如果没有前一步柯理化 , CreateValidateExpression 的第二个参数 validateFuncExpression 将很难确定 。
  3. CreateValidateStringRequiredExpression 和 CreateValidateStringMinLengthExpression 内部调用了 CreateValidateExpression , 但是固定了几个参数 。 这其实也可以被认为是一种柯理化 , 因为返回值是表达式其实可以被认为是一种函数的表现形式 , 当然理解为重载也没有问题 , 不必太过纠结 。
第九步 , 支持更多模型到现在 , 我们已经得到了一个支持验证 CreateClaptrapInput 多个 string 字段的验证器 。 并且 , 即使要扩展多更多类型也不是太难 , 只要增加表达式即可 。
本节 , 我们将 CreateClaptrapInput 抽象为更抽象的类型 , 毕竟没有模型验证器是专门只能验证一个 class 的 。
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Diagnostics;using System.Linq;using System.Linq.Expressions;using System.Reflection;using FluentAssertions;using NUnit.Framework;// ReSharper disable InvalidXmlDocCommentnamespace Newbe.ExpressionsTests{////// Multiple Type///public class X03PropertyValidationTest09{private const int Count = 10_000;private static readonly Dictionary> ValidateFunc =new Dictionary>();[SetUp]public void Init(){try{var finalExpression = CreateCore(typeof(CreateClaptrapInput));ValidateFunc[typeof(CreateClaptrapInput)] = finalExpression.Compile();Expression> CreateCore(Type type){// exp for inputvar inputExp = Expression.Parameter(typeof(object), "input");// exp for outputvar resultExp = Expression.Variable(typeof(ValidateResult), "result");// exp for return statementvar returnLabel = Expression.Label(typeof(ValidateResult));var innerExps = new List {CreateDefaultResult()};var stringProps = type.GetProperties().Where(x => x.PropertyType == typeof(string));foreach (var propertyInfo in stringProps){if (propertyInfo.GetCustomAttribute() != null){innerExps.Add(CreateValidateStringRequiredExpression(propertyInfo));}var minlengthAttribute = propertyInfo.GetCustomAttribute();if (minlengthAttribute != null){innerExps.Add(CreateValidateStringMinLengthExpression(propertyInfo, minlengthAttribute.Length));}}innerExps.Add(Expression.Label(returnLabel, resultExp));// build whole blockvar body = Expression.Block(new[] {resultExp},innerExps);// build lambda from bodyvar final = Expression.Lambda


推荐阅读