>(body,inputExp);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)=> CreateValidateExpression(propertyInfo,CreateValidateStringRequiredExp());Expression CreateValidateStringMinLengthExpression(PropertyInfo propertyInfo,int minlengthAttributeLength)=> CreateValidateExpression(propertyInfo,CreateValidateStringMinLengthExp(minlengthAttributeLength));Expression CreateValidateExpression(PropertyInfo propertyInfo,Expression validateFuncExpression){var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk));Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null");var convertedExp = Expression.Convert(inputExp, type);var namePropExp = Expression.Property(convertedExp, 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}public static ValidateResult Validate(CreateClaptrapInput input){return ValidateFunc[typeof(CreateClaptrapInput)].Invoke(input);}}}代码要点:
- 将 Func 替换为了 Func
- 将对应类型的验证器创建好之后保存在 ValidateFunc 中 。 这样就不需要每次都重建整个 Func 。
第十步 , 加入一些细节最后的最后 , 我们又到了令人愉快的 “加入一些细节” 阶段:按照业务特性对抽象接口和实现进行调整 。 于是我们就得到了本示例最终的版本 。
using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Diagnostics;using System.Linq;using System.Linq.Expressions;using System.Reflection;using Autofac;using FluentAssertions;using NUnit.Framework;using Module = Autofac.Module;// ReSharper disable InvalidXmlDocCommentnamespace Newbe.ExpressionsTests{////// Final///public class X03PropertyValidationTest10{private const int Count = 10_000;private IValidatorFactory _factory = null!;[SetUp]public void Init(){try{var builder = new ContainerBuilder();builder.RegisterModule();var container = builder.Build();_factory = container.Resolve();}catch (Exception e){Console.WriteLine(e);throw;}}[Test]public void Run(){for (int i = 0; i
推荐阅读
-
『中国知识产权报』6:超万亿美元背后的专利博弈,Wi-Fi
-
-
-
-
-
西九条美优子|第二阶段不容犯错,万博卡帅研究B组录像对华夏中超国安了如指掌
-
-
社长财经|步步为赢,炒股原来最重要的并不是赚钱!走好第1步
-
上观|跑友兴奋了!2020闻泰安世上海10公里精英赛10月18日开启
-
文汇报|申城11日迎今夏第11个高温日 12日起九天还将持续高温
-
-
穿搭|优衣库大促,还有这些好物值得买!这样搭配超适合现在的心情
-
央视财经|坐地起价?!说好260元搬家费,现场却被索要5000元!“套路搬”频现,背后暗藏利益链……
-
疫情或令1亿人重返极端贫困|世行行长:疫情或令1亿人重返极端贫困 可能进一步恶化
-
医说健康帮你轻松瘦肚子,方法简单让你轻松瘦下来,提醒:5个小动作
-
-
历史其实真的很好看|灌州故事:清朝灌县第一任知县—马玑
-
『欲求木之长』上桌满屋飘香,满满的全是小时候妈妈的味道,口水不自觉的往下流
-
奔跑吧兄弟|《跑男》baby公主特辑,将镜头放大十倍,这小腿肌是认真的吗?
-