C#判斷語句的表達式樹實現(xiàn)
C# 提供了以下類型的判斷語句:
| 語句 | 描述 |
|---|---|
| if | 一個 if 語句 由一個布爾表達式后跟一個或多個語句組成。 |
| if...else | 一個 if 語句 后可跟一個可選的 else 語句,else 語句在布爾表達式為假時執(zhí)行。 |
| 嵌套 if 語句 | 您可以在一個 if 或 else if 語句內(nèi)使用另一個 if 或 else if 語句。 |
| switch 語句 | 一個 switch 語句允許測試一個變量等于多個值時的情況。 |
| 嵌套 switch 語 | 您可以在一個 switch 語句內(nèi)使用另一個 switch 語句。 |
當(dāng)然還有 ??、?: 等判斷,下面將詳細實踐。
if
If 語句,使用 IfThen(Expression test, Expression ifTrue); 來表達
Expression test表示用于判斷的表達式,Expression ifTrue表示結(jié)果為 true 時執(zhí)行的表達式樹。
示例
int a = 10;
int b = 10;
if (a == b)
{
Console.WriteLine("a == b 為 true,語句被執(zhí)行");
}
Console.ReadKey();使用表達式樹實現(xiàn)如下
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,表達式樹被執(zhí)行"));
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,表達式樹被執(zhí)行")
} .Else {
.Default(System.Void)
}
}if...else
if...else 使用以下表達式樹表示
ConditionalExpression IfThenElse(Expression test, Expression ifTrue, Expression ifFalse);
示例代碼如下
int a = 10;
int b = 11;
if (a == b)
{
Console.WriteLine("a == b 為 true,此語句被執(zhí)行");
}
else
{
Console.WriteLine("a == b 為 false,此語句被執(zhí)行");
}
Console.ReadKey();用表達式樹實現(xiàn)如下
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,此表達式樹被執(zhí)行"));
MethodCallExpression call2 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == b 為 false,此表達式樹被執(zhí)行"));
ConditionalExpression _if = Expression.IfThenElse(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.WriteLine("a == b 為 true,此表達式樹被執(zhí)行")
} .Else {
.Call System.Console.WriteLine("a == b 為 false,此表達式樹被執(zhí)行")
}
}switch
示例代碼如下
int a = 2;
switch (a)
{
case 1:Console.WriteLine("a == 1");break;
case 2:Console.WriteLine("a == 2");break;
default:Console.WriteLine("a != 1 && a = 2");
}
Console.ReadKey();每個 case 使用 SwitchCase 類型表示,使用 Expression.SwitchCase 生成 SwitchCase 類型。
Expression.Switch 用來構(gòu)建一個 switch 表達式樹,
Expression.Switch 的重載比較多,常用的是這種形式
SwitchExpression Switch(Expression switchValue, Expression defaultBody, params SwitchCase[] cases);
switchValue 表示傳入?yún)?shù);
defaultBody 表示 default 執(zhí)行的表達式;
cases 表示多條 case 。
上面代碼對應(yīng)使用表達式樹編寫如下
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"));
SwitchCase case1 = Expression.SwitchCase(
Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == 1")),
new ConstantExpression[] { Expression.Constant(1) }
);
SwitchCase case2 = Expression.SwitchCase(
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 SwitchCase[] { case1, case2 });
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) {
.Case (1):
.Call System.Console.WriteLine("a == 1")
.Case (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 ifFalse)
可以參考上面的 if...else 表達式樹,這里不再贅述。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#表達式樹基礎(chǔ)教程
- C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)
- C#五類運算符使用表達式樹進行操作
- C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值(示例代碼)
- C#表達式樹講解
- C#使用表達式樹實現(xiàn)對象復(fù)制的示例代碼
- C#表達式樹Expression基礎(chǔ)講解
- C# Lambda表達式及Lambda表達式樹的創(chuàng)建過程
- C#用表達式樹構(gòu)建動態(tài)查詢的方法
- C#表達式樹的基本用法講解
- 淺談c#表達式樹Expression簡單類型比較demo
- C# 表達式樹Expression Trees的知識梳理
- C#之Expression表達式樹實例
- C#值類型、引用類型、泛型、集合、調(diào)用函數(shù)的表達式樹實踐
相關(guān)文章
C# menuStrip控件實現(xiàn)鼠標(biāo)滑過自動彈出功能
MenuStrip 控件是 Visual Studio 和 .NET Framework 中的功能。使用該控件,可以輕松創(chuàng)建 Microsoft Office 中那樣的菜單。本文給大家分享menuStrip鼠標(biāo)滑過自動彈出效果2021-07-07
C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能
一直喜歡用Google Translate API進行在線翻譯,但是服務(wù)越來越慢這篇文章,所以只能換一個了,主要給大家介紹了關(guān)于C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能的相關(guān)資料,需要的朋友可以參考下2021-06-06
.NET實現(xiàn):將EXE設(shè)置開機自動啟動
.NET實現(xiàn):將EXE設(shè)置開機自動啟動的方法,需要的朋友可以參考一下2013-03-03

