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}}}代码要点:
- CreateValidateExpression 就是被抽离出来的公共方法 。
- 如果没有前一步柯理化 , CreateValidateExpression 的第二个参数 validateFuncExpression 将很难确定 。
- 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
推荐阅读
-
直播吧|我连面试维拉的机会都没有,约克:黑人球员想从事执教很困难
-
-
-
空调|消费者苦不堪言!空调售后收费问题多多:乱象背后大揭秘
-
-
达斐丽|演绎优雅气质的现代美学,123m2现代美式
-
分身人:电影《分身人》(Free Guy)发布新特辑
-
-
『在人群中寻找你』网友恍然大悟,郑爽为啥笑起来从不露牙呢?看到她的牙齿后
-
「布丁不语」盘点适合设计师的平板电脑,随时记录灵感
-
湖人队|湖人迎来1好1坏?沃格尔赞扬25+10超巨,7+5后卫正式叫板浓眉哥
-
-
车臻:毁了多少人!,中国式婚恋现状:“对我好”这三个字
-
证券时报|2020服贸会:释放中国坚持经济全球化“最强音”
-
越窑青瓷|有个博物馆在梁弄开馆,故宫博物院专门发来贺信!
-
-
「朱顶红」朱顶红种球栽植养护有方法,花大色艳根系壮,年年分生小球
-
篮筐之下|布朗教练被解雇,泰伦卢成为候选主教练,76人被横扫
-
央视新闻客户端|南非主流媒体发文:“一带一路”倡议将成为全球经济复苏催化剂
-
【日本】未来30年谁能与美国抗衡?专家给出预测:这3个国家最有可能