>(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 CreateValidateNameRequiredExpression(){var requireMethod = typeof(X03PropertyValidationTest03).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 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);/*** final as:* result = ValidateNameRequired("Name", input.Name);* if (!result.IsOk)* {*return result;* }*/return re;}Expression CreateValidateNameMinLengthExpression(){var minLengthMethod =typeof(X03PropertyValidationTest03).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 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);/*** final as:* result = ValidateNameMinLength("Name", input.Name, minLength);* if (!result.IsOk)* {*return result;* }*/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 < minLength? ValidateResult.Error($"Length of {name} should be great than {minLength}"): ValidateResult.Ok();}}}代码要点:
- 正如前文所述 , 我们修改了 ValidateNameRequired, 并重命名为 ValidateStringRequired 。ValidateNameMinLength -> ValidateStringMinLength 。
推荐阅读
-
吃货|自带香味与BMG的神仙广式啫啫煲?今天我总算是见识到了
-
-
-
干玻璃胶怎么去除小窍门图解 干玻璃胶怎么去除小窍门
-
瀚海狼山 全怨年景不好?,某音裁员1.2万人摇摇欲坠
-
-
-
文学|张爱玲三年搬家180次,身患皮肤病只是表因,难言之隐让人心酸
-
全国能源信息平台|价差-30厘/千瓦时!广东2020年7月集中竞价交易电量26.86亿千瓦时
-
『杨幂』杨幂对baby开另类玩笑不算啥,看她对黄晓明举动,网友: 厉害了
-
「新车」国产再添“新颜兽”新车比思域还时尚,配名牌1.5T+LED,仅8万起
-
干部脱贫攻坚心得体会2020年?打赢脱贫攻坚战的心得体会800_1
-
手机中国|X1 Fold、X1 Nano发布,可折叠的笔记本!联想ThinkPad
-
-
|都怪老公没本事买不起大房子,婚房只住了2年,堆积得像猪窝
-
水浒后传:浪子燕青营救关胜暗藏啥玄机?饮马川群雄再聚义!
-
-
『日落西山吖』骁龙855+UFS3.0,6.5英寸极边全面屏,魅族手机
-
|这四大行业必将崛起!聪明人早已开始布局!,疫情过后
-
小婕爱穿搭|小婕爱穿搭:小姐姐穿粉色露肩上衣搭配黑色短裤,可盐可甜