Expand Minimize
本文章是由機器翻譯。 將指標移到文章內的文字上方即可查看原文。 其他資訊。
譯文
原文
本主題尚未接受評分 - 為這個主題評分

Expression.IfThenElse 方法

建立 ConditionalExpression,表示具有 ifelse 陳述式的條件區塊。

命名空間:  System.Linq.Expressions
組件:  System.Core (在 System.Core.dll 中)
public static ConditionalExpression IfThenElse(
	Expression test,
	Expression ifTrue,
	Expression ifFalse
)

參數

test
型別:System.Linq.Expressions.Expression
要將 Test 屬性設定為與之相等的 Expression
ifTrue
型別:System.Linq.Expressions.Expression
要將 IfTrue 屬性設定為與之相等的 Expression
ifFalse
型別:System.Linq.Expressions.Expression
要將 IfFalse 屬性設定為與之相等的 Expression

傳回值

型別:System.Linq.Expressions.ConditionalExpression
ConditionalExpression ,其 NodeType 屬性等於 Conditional,且 TestIfTrueIfFalse 屬性設定為指定的值。 這個方法所傳回之結果 ConditionalExpression 的型別為 Void

下列程式碼範例示範如何建立表示條件區塊的運算式。


// Add the following directive to the file:
// using System.Linq.Expressions;  
bool test = true;

// This expression represents the conditional block.
Expression ifThenElseExpr = Expression.IfThenElse(
    Expression.Constant(test),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("The condition is true.")
    ),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("The condition is false.")
    )
);

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(ifThenElseExpr).Compile()();

// This code example produces the following output:
//
// The condition is true.


.NET Framework

支援版本:4.5、4

.NET Framework Client Profile

支援版本:4

可攜式類別庫

支援版本:可攜式類別庫

適用於 Windows 市集應用程式的 .NET

支援版本:Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (不支援伺服器核心角色), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心角色,不支援 Itanium)

.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求
本文對您有任何幫助嗎?
(剩餘 1500 個字元)

社群新增項目

新增
Microsoft 正展開一份線上問卷調查,了解您對於 MSDN 網站的看法。 如果您選擇參加,您離開 MSDN 網站時即會顯示線上問卷調查。

您是否想要參加?
© 2013 Microsoft. 著作權所有,並保留一切權利。