>(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 。
推荐阅读
-
『核武器』在核武器“威胁”下的和平,真的算得上是真正的和平吗?
-
-
-
-
-
-
中国新闻网|谭耀宗:全国人大常委会决定令香港能够集中精力抗疫
-
澎湃新闻|建设银行一口气补齐两位副行长:吕家进、王浩履新
-
-
[时尚迪科]叫“精致”A字裙,比真丝洋气,任谁见都想多看几眼,发现一女装
-
-
净流入|7月8日恒生指数收盘上涨0.59%,南向资金当日净流入103.25亿元
-
-
俊妈故事会|准妈妈也该好好休息,别打扰宝宝发育,这3个时间是胎儿睡眠时间
-
姚哥乡村行1|营养丰富制作方法简单易学,孩子能多吃碗饭,味道正宗好吃下饭
-
-
北晚新视觉网|医生5分钟取出,太危险!10个月大婴儿误食3厘米长别针
-
-
肺癌▲我国每年80万人患上肺癌,身上有4处“变大”,一定要重视
-