>(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 。
推荐阅读
-
-
花大色艳|花大色艳花期长,满屋芳香四溢,养在家里超合适的
-
-
橘子味的玫瑰挞|汁浓醇香可口,肥而不腻!多配两碗米饭都不够啊,红烧肉朴实易做
-
大唐|梦幻西游:年内第一109大唐,创号2年全身5件无级别,区内无敌!
-
地方|老祖宗的告诫:有两个地方尽量别去,去了,就容易招灾惹祸
-
庞妈军视野■56架飞机直飞中方:救命物资不容有失,6000万只口罩被“截胡”
-
-
寸寸旧时光|靳东妻子晒照为小儿子庆生 兄弟俩背影出镜超可爱
-
5G手机百元5G手机,将卢伟冰逼到了墙角!还顺便"踩"了联发科一脚
-
美国女巫|又希望被关心的星座,他们有多重人格,天秤座上榜双子座天秤座摩羯座,不想被打扰
-
-
-
东方网|国庆假期首日上海机场、火车站很忙碌:虹桥站车票售罄 航司开启“加班模式”
-
-
-
中金网|/ 美元在测试月度高点后形成盘整形态 0.7340 一线难破?,澳元
-
-
山岭|湖南现“鬼崽岭”,雨天冲出阴兵石人,专家:规模超秦兵马
-
孩子过于追求完美,父母可用三个方法,帮助孩子度过“敏感期”