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
推荐阅读
-
灵锦文化假期结束A股市场走向不明,智能包装龙头完成数字化转型
-
星话天天看|事业一帆顺风,鸿运驾到,日子无忧发大财的属相,10月1-8号
-
-
丰田卡罗拉|拉人拉货两不误!卡罗拉旅行版发布,自带双人床,国产或12万起
-
图书馆里真的有早上进馆一直做题晚上闭馆才出去 ,午饭都常不吃的学生吗
-
-
雍亲王府|但康熙还是有传位给雍正的明显征兆,雍正虽不是康熙最心仪的人选
-
露笑科技|第三代半导体核心个股,露笑科技有这个底气吗?
-
王小二君临天下▲F-22紧急升空拦截,突发!两架他国军机突然闯入美国防空识别区
-
-
中芯国际|大资金都开始“避险”了,散户却依然在“狂欢”,这意味着什么?
-
萌宠的小世界|跑别人家去现在不肯走了 我问大家怎么办,聪明金毛记错自家楼层
-
-
上海虹口门户网站|区科委党委运用“O2O”模式开展“四史”学习教育
-
-
金杰娱乐|李小龙死在她床上,这个叫丁佩的女人有多狠?,20岁以性感出道
-
-
「华丽志」推出公益APP,为法国医护人员提供免费住房,Airbnb
-
-
天涯明月刀|3款近期超人气全新手游,天刀原神只是开胃菜,街霸对决才是C位