修改了 CreateValidateNameRequiredExpression 和 CreateValidateNameMinLengthExpression , 因为静态方法的参数发生了变化 。 通过这样的改造 , 我们便可以将两个静态方法用于更多的属性验证 。 读者可以尝试增加一个 NickName 属性 。 并且进行相同的验证 。 第四步 , 支持多个属性验证接下来 , 我们通过将验证 CreateClaptrapInput 所有的 string 属性 。
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{////// Reflect Properties///public class X03PropertyValidationTest04{private const int Count = 10_000;private static Func _func;[SetUp]public void Init(){try{var finalExpression = CreateCore();_func = finalExpression.Compile();Expression> CreateCore(){// exp for inputvar inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input");var minLengthPExp = Expression.Parameter(typeof(int), "minLength");// 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 = typeof(CreateClaptrapInput).GetProperties().Where(x => x.PropertyType == typeof(string));foreach (var propertyInfo in stringProps){innerExps.Add(CreateValidateStringRequiredExpression(propertyInfo));innerExps.Add(CreateValidateStringMinLengthExpression(propertyInfo));}innerExps.Add(Expression.Label(returnLabel, resultExp));// build whole blockvar body = Expression.Block(new[] {resultExp},innerExps);// build lambda from bodyvar final = Expression.Lambda>(body,inputExp,minLengthPExp);return final;Expression CreateDefaultResult(){var okMethod = typeof(ValidateResult).GetMethod(nameof(ValidateResult.Ok));Debug.Assert(okMethod != null, nameof(okMethod) + " != null");var methodCallExpression = Expression.Call(okMethod);var re = Expression.Assign(resultExp, methodCallExpression);/*** final as:* result = ValidateResult.Ok()*/return re;}Expression CreateValidateStringRequiredExpression(PropertyInfo propertyInfo){var requireMethod = typeof(X03PropertyValidationTest04).GetMethod(nameof(ValidateStringRequired));var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk));Debug.Assert(requireMethod != null, nameof(requireMethod) + " != null");Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null");var namePropExp = Expression.Property(inputExp, propertyInfo);var nameNameExp = Expression.Constant(propertyInfo.Name);var requiredMethodExp = Expression.Call(requireMethod, 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;}Expression CreateValidateStringMinLengthExpression(PropertyInfo propertyInfo){var minLengthMethod =typeof(X03PropertyValidationTest04).GetMethod(nameof(ValidateStringMinLength));var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk));Debug.Assert(minLengthMethod != null, nameof(minLengthMethod) + " != null");Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null");var namePropExp = Expression.Property(inputExp, propertyInfo);var nameNameExp = Expression.Constant(propertyInfo.Name);var requiredMethodExp = Expression.Call(minLengthMethod,nameNameExp,namePropExp,minLengthPExp);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}public static ValidateResult Validate(CreateClaptrapInput input){return _func.Invoke(input, 3);}public static ValidateResult ValidateStringRequired(string name, string value){return string.IsNullOrEmpty(value)? ValidateResult.Error($"missing {name}"): ValidateResult.Ok();}public static ValidateResult ValidateStringMinLength(string name, string value, int minLength){return value.Length
推荐阅读
-
防控|辽宁大连将甘井子区大连湾街道划定为高风险地区
-
|在macOS Big Sur中发现的代码表明Face ID可能会出现在Mac上
-
-
-
Windows操作系统如何快速激活?4行代码轻松搞定
-
请各位大佬推荐北京靠谱的心理咨询机构,价格在每小时1000左右?
-
【iPhone SE】三千块买iPhoneSE还是买国产手机?一张图让你看清楚!
-
「果粉之家0」大屏机仍然是主流选择,小屏机仅占10%
-
『鲸鱼爱吃栗子』穿粉嫩紧身长裙勒身材,表情有些僵硬,于明加“沙漏身材”好抢镜
-
RNG|RNG再爆卡合同丑闻,迫害前员工不让找工作,逼迫其退出电竞圈
-
断肠人说动漫|运势如雨,富贵挡不住的四大属相,8月中旬开始事业一路畅通
-
-
「iphone12」iPhone 12或有4G和5G两个版本;华为Mate40系列屏幕规格曝光
-
-
-
第一财经|美国企业对特定海绵或泡沫产品提起337调查申请
-
姹紫嫣红|20年291期姹紫嫣红3d一句定三码原创
-
直击李玟葬礼:成龙刘德华送感言,韩红现身靠搀扶,二姐微笑献唱
-
-