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
推荐阅读
-
-
-
中国新闻网|中国心血管病患者达3.3亿 这些不良习惯你占了几个?
-
射手座|会生活,什么情况下都能让自己活得很好的星座!
-
游戏主|国庆后线下奔现,看到女友1张聊天截图,酸了,王者荣耀小伙网恋一年半
-
「史蒂夫·纳什」巅峰期的史蒂夫-纳什究竟恐怖在什么地方?
-
虞姬|伽罗垫底,后羿已凉,新版本射手一哥确定,自带2件名刀司命
-
-
-
都说金庸偏爱郭襄,为何却给了郭芙完美的婚姻,徒留郭襄一人孤独
-
新手6不6|参考这些赚钱项目,创业课堂:分析工资不够花怎么办
-
魔兽|《魔兽世界》能免费变更角色性别了:随新资料片《暗影国度》上线
-
-
小米科技|华为Nova8 SE海报曝光 浴霸四摄+直角中框 网友调侃:致敬苹果?
-
直播吧:大家在群聊时会关心他,坦普尔:KD感染新冠让全队感到惊讶
-
-
科技六分钟|骁龙865+256GB+4700mAh+30倍数码变焦,比6.18更实惠,仅3258元
-
-
-