c# 提供了以下类型的判断语句:
当然还有??
、?:
等判断,下面将详细实践。
if 语句,使用ifthen(expression test, expression iftrue);
来表达
expression test
表示用于判断的表达式,expression iftrue
表示结果为 true 时执行的表达式树。
示例
int a = 10; int b = 10; if (a == b) { console.writeline("a == b 为 true,语句被执行"); } console.readkey();
使用表达式树实现如下
parameterexpression a = expression.variable(typeof(int), "a"); parameterexpression b = expression.variable(typeof(int), "b"); methodcallexpression call = expression.call( null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.constant("a == b 为 true,表达式树被执行")); conditionalexpression _if = expression.ifthen(expression.equal(a, b),call); expression<action<int, int>> lambda = expression.lambda<action<int, int>>(_if,a,b); lambda.compile()(10,10); console.readkey();
生成的表达式树如下
.lambda #lambda1<system.action`2[system.int32父爱昼夜无眠,system.int32]>( system.int32 $a, system.int32 $b) { .if ($a == $b) { .call system.console.writeline("a == b 为 true,表达式树被执行") } .el { .default(system.void) }}
if…el 使用以下表达式树表示
conditionalexpression ifthenel(expression test, expression iftrue, expression iffal);
示例代码如下
int a = 10; int b = 11; if (a == b) { console.writeline("a == b 为 true,此语句被执行"); } el { console.writeline("a == b 为 fal,此语句被执行"); } console.readkey();
用表达式树实现如下
parameterexpression a = expression.variable(typeof(int), "a"); parameterexpression b = expression.variable(typeof(int), "b"); methodcallexpression call1 = expression.call( 精益求精的例子 null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.constant("a == b 为 true,此表达式树被执行")); methodcallexpression call2 = expression.call( null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.constant("a == b 为 fal,此表达式树被执行")); conditionalexpression _if = expression.ifthenel(expression.equal(a, b), call1,call2); expression<action<int, int>> lambda = expression.lambda<action<int, int>>(_if, a, b); lambda.compile()(10, 11); console.readkey();
生成的表达式树如下
.lambda #lambda1<system.action`2[system.int32,system.int32]>( system.int32 $a, system.int32 $b) { .if ($a == $b) { .call system.console.w读书小报是什么riteline("a == b 为 true,此表达式树被执行") } .el { .call system.console.writeline("a == b 为 fal,此表达式树被执行") }}
示例代码如下
int a = 2; switch (a) { ca 1:console.writeline("a == 1");break; ca 2:console.writeline("a == 2");break; default:console.writeline("a != 1 && a = 2"); } console.readkey();
每个 ca 使用 switchca 类型表示,使用 expression.switchca 生成 switchca 类型。
expression.switch 用来构建一个 switch 表达式树,
expression.switch 的重载比较多,常用的是这种形式
switchexpression switch(expression switchvalue, expression defaultbody, params switchca[] cas);
switchvalue 表示传入参数;
defaultbody 表示 default 执行的表达式;
cas 表示多条 ca 。
上面代码对应使用表达式树编写如下
parameterexpression a = expression.parameter(typeof(int), "a"); methodcallexpression _default = expression.call( null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.constant("a != 1 && a = 2")); switchca ca1 = expression.switchca( expression.call(null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.消防安全工作总结constant("a == 1")), new constantexpression[] { expression.constant(1) } ); switchca ca2 = expression.switchca( expression.call(null, typeof(console).getmethod("writeline", new type[] { typeof(string) }), expression.constant("a == 2")), new constantexpression[] { expression.constant(2) } ); switchexpression _switch = expression.switch(a, _default, new switchca[] { ca1, ca2 }); expression<action<int>> lambda = expression.lambda<action<int>>(_switch, a); lambda.compile()(1); console.readkey();
生成的表达式树如下
.lambda #lambda1<system.action`1[system.int32]>(system.int32 $a) { .switch ($a) { .ca (1): .call system.console.writeline("a == 1") .ca (2): .call system.console.writeline("a == 2") .default: .call system.console.writeline("a != 1 && a = 2") }}
很奇怪,没有 break,但是表达式树是正常的,并且运行没问题;
?? 表示空合并运算符,例如a ?? b
,如果 a 不为 null,即返回 a,否则返回 b;
常用定义如下
binaryexpression coalesce(expression left, expression right)
这里就不再赘述。
?: 是三元运算符,例如 a > b ? a : b 。
常用定义如下
conditionalexpression condition(expression test, expression iftrue, expression iffal)
可以参考上面的 if…el 表达式树,这里不再赘述。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 15:55:38,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/bd40a0c776fde4c7bb6f0103ad51207a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#判断语句的表达式树实现.doc
本文 PDF 下载地址:C#判断语句的表达式树实现.pdf
留言与评论(共有 0 条评论) |