>(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 。
推荐阅读
-
「特种兵」现实版的“士兵突击”:他打败欧美13支特种兵,获2次个人一等功
-
高考|高考试卷一公布,就知道原来初中这么重要,扎实基础按这四点做起
-
时尚|赌王葬礼:原配家成员打扮最随意,有人红头发有人穿背心,究竟哪来的底气?
-
花同学爱拍照|1.78m美女的牛仔优雅风穿搭,腿长一米不是梦
-
黎贝卡的异想世界|谁说鼻子塌就不好看了!她们刷新了我对鼻子美的认知
-
涂涂画画就能和阿尔法蛋互动游戏,发散孩子思维,轻轻松松学编程
-
-
-
荣耀手机|6400万广角长焦微距四摄,8+128GB+40W华为中端机已降至2099
-
-
『手机大魔王』小米新系统引发恐慌,如果系统不安全,你还会继续用吗?
-
美国|确诊病例上升100万仅用17天,美国专家反思疫情应对
-
-
-
陈萌|陈萌自曝被闺蜜劝分,为朱小伟跟其断绝来往,称防火防盗防闺蜜
-
-
itMode|马丁靴+裙子,马丁靴+工装裤:又酷又撩,时髦炸了!
-
科技天天侃 华为无奈接受现实,后路还有一条,雪上加霜!联发科突然宣布
-
-
娱乐掮客|前面一美女点了一碗水饺。,幽默笑话:在快餐店排队