修改了 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
推荐阅读
-
吴亦凡|管不好下半身,就注定过不好下半生。刚过完32岁生日吴亦凡迎来13年的铁窗生涯
-
-
-
『钢铁侠』《钢铁侠》你不一定知道的幕后故事,说出来你别不信
-
王宝强|13号台风鲸鱼形成,会不会来干扰十一长假?权威预报:放一百个心
-
-
『忘川秋水』这台进口拖挂也能住7人!HOBBY515UHK,除了营地房车
-
游资|太惨了!昨天吃到“天地板”,今天遭遇“一字板”跌停,游资出逃超1.6亿元,来看看到底发生了什么?
-
江疏影|华表奖现场吃瓜:王一博裤子,易烊千玺王俊凯座位,江疏影最丢脸
-
国六:新款本田冠道本月上市,取消1.5T最低配车型,2.0T国六终于来了
-
皮肤|王者:1688点券的皮肤,贵有贵的好处,百万玩家期待这款再次返场
-
|中国是最大买家,澳大利亚再迎坏消息:又一“最赚钱的生意”受创
-
曼联|惨烈!曼联两大后卫上演头撞头,8千万巨星当场染血,队友被抬走!
-
『陈冰』她瞒着老公参加《中国好声音》,爆红后火速离婚,如今落魄无人识
-
-
那些颜值高的明星们在拍烂片时,面对弱智情节傻逼台词究竟是咋想的咋做到的?
-
四川男篮|8战6胜复赛最大黑马!一人缔造24-2攻势,王哲林低迷福建轻取四川
-
央视@该院已被关停,印度孟买一医院29名医护人员感染新冠
-
朋友生宝宝送什么礼物-朋友生孩子送什么礼物-朋友生宝宝去看望买什么东西
-
子非鱼|为什么不爱保养的女性皮肤却最好?许多人都不懂,这才是真正原因