Expression.Add 方法
定義
建立 BinaryExpression,其代表不含溢位檢查的算術加法運算。Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking.
多載
Add(Expression, Expression) |
建立 BinaryExpression,其代表不含溢位檢查的算術加法運算。Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking. |
Add(Expression, Expression, MethodInfo) |
建立 BinaryExpression,其代表不含溢位檢查的算術加法運算。Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking. 實作的方法可加以指定。The implementing method can be specified. |
Add(Expression, Expression)
建立 BinaryExpression,其代表不含溢位檢查的算術加法運算。Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking.
public:
static System::Linq::Expressions::BinaryExpression ^ Add(System::Linq::Expressions::Expression ^ left, System::Linq::Expressions::Expression ^ right);
public static System.Linq.Expressions.BinaryExpression Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right);
static member Add : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.BinaryExpression
Public Shared Function Add (left As Expression, right As Expression) As BinaryExpression
參數
- left
- Expression
要將 Expression 屬性設定為與之相等的 Left。A Expression to set the Left property equal to.
- right
- Expression
要將 Expression 屬性設定為與之相等的 Right。A Expression to set the Right property equal to.
傳回
BinaryExpression,其 NodeType 屬性等於 Add,且 Left 和 Right 屬性設定為指定的值。A BinaryExpression that has the NodeType property equal to Add and the Left and Right properties set to the specified values.
例外狀況
left
或 right
為 null
。left
or right
is null
.
不會為 left
.Type 和 right
.Type 定義 Addition 運算子。The addition operator is not defined for left
.Type and right
.Type.
範例
下列程式碼範例示範如何建立運算式,以新增兩個整數。The following code example shows how to create an expression that adds two integers.
// Add the following directive to your file:
// using System.Linq.Expressions;
// This expression adds the values of its two arguments.
// Both arguments must be of the same type.
Expression sumExpr = Expression.Add(
Expression.Constant(1),
Expression.Constant(2)
);
// Print out the expression.
Console.WriteLine(sumExpr.ToString());
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(Expression.Lambda<Func<int>>(sumExpr).Compile()());
// This code example produces the following output:
//
// (1 + 2)
// 3
' Add the following directive to your file:
' Imports System.Linq.Expressions
' This expression adds the values of its two arguments.
' Both arguments must be of the same type.
Dim sumExpr As Expression = Expression.Add(
Expression.Constant(1),
Expression.Constant(2)
)
' Print the expression.
Console.WriteLine(sumExpr.ToString())
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
Console.WriteLine(Expression.Lambda(Of Func(Of Integer))(sumExpr).Compile()())
' This code example produces the following output:
'
' (1 + 2)
' 3
備註
產生的 BinaryExpression 屬性會 Method 設定為實方法。The resulting BinaryExpression has the Method property set to the implementing method. Type屬性會設定為節點的型別。The Type property is set to the type of the node. 如果節點已提升, IsLifted 和 IsLiftedToNull 屬性都是 true
。If the node is lifted, the IsLifted and IsLiftedToNull properties are both true
. 否則為 false
。Otherwise, they are false
. Conversion 屬性為 null
。The Conversion property is null
.
下列資訊描述實方法、節點型別,以及是否要提升節點。The following information describes the implementing method, the node type, and whether a node is lifted.
實方法Implementing Method
下列規則會決定作業的選定實方法:The following rules determine the selected implementing method for the operation:
如果 Type 或的屬性代表多載
left
right
加法運算子的使用者定義型別, MethodInfo 表示該方法的會是實方法。If the Type property of eitherleft
orright
represents a user-defined type that overloads the addition operator, the MethodInfo that represents that method is the implementing method.否則為
left
。輸入和right
。類型為數數值型別,實方法為null
。Otherwise, ifleft
.Type andright
.Type are numeric types, the implementing method isnull
.
節點類型和提升與未提升Node Type and Lifted versus Non-Lifted
如果執行中的方法不是 null
:If the implementing method is not null
:
如果為,則為
left
。輸入和right
。類型可指派給實方法的對應引數類型,而不會引發節點。Ifleft
.Type andright
.Type are assignable to the corresponding argument types of the implementing method, the node is not lifted. 節點的型別是實方法的傳回型別。The type of the node is the return type of the implementing method.如果符合下列兩個條件,節點就會被提升,而節點的類型會是可為 null 的型別,該型別會對應至實方法的傳回型別:If the following two conditions are satisfied, the node is lifted and the type of the node is the nullable type that corresponds to the return type of the implementing method:
left
.輸入和right
。類型是數值型別,其中至少有一個可為 null,而對應的不可為 null 的類型等於實方法的對應引數類型。left
.Type andright
.Type are both value types of which at least one is nullable and the corresponding non-nullable types are equal to the corresponding argument types of the implementing method.實方法的傳回型別是不可為 null 的實值型別。The return type of the implementing method is a non-nullable value type.
如果實方法為 null
:If the implementing method is null
:
如果為,則為
left
。輸入和right
。類型不可為 null,節點不會被提升。Ifleft
.Type andright
.Type are both non-nullable, the node is not lifted. 節點的型別是預先定義的加法運算子的結果型別。The type of the node is the result type of the predefined addition operator.如果為,則為
left
。輸入和right
。型別可以是可為 null,而是會提升節點。Ifleft
.Type andright
.Type are both nullable, the node is lifted. 節點的型別是可為 null 的型別,它會對應到預先定義的加法運算子的結果型別。The type of the node is the nullable type that corresponds to the result type of the predefined addition operator.
適用於
Add(Expression, Expression, MethodInfo)
建立 BinaryExpression,其代表不含溢位檢查的算術加法運算。Creates a BinaryExpression that represents an arithmetic addition operation that does not have overflow checking. 實作的方法可加以指定。The implementing method can be specified.
public:
static System::Linq::Expressions::BinaryExpression ^ Add(System::Linq::Expressions::Expression ^ left, System::Linq::Expressions::Expression ^ right, System::Reflection::MethodInfo ^ method);
public static System.Linq.Expressions.BinaryExpression Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo method);
public static System.Linq.Expressions.BinaryExpression Add (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Reflection.MethodInfo? method);
static member Add : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Reflection.MethodInfo -> System.Linq.Expressions.BinaryExpression
Public Shared Function Add (left As Expression, right As Expression, method As MethodInfo) As BinaryExpression
參數
- left
- Expression
要將 Expression 屬性設定為與之相等的 Left。A Expression to set the Left property equal to.
- right
- Expression
要將 Expression 屬性設定為與之相等的 Right。A Expression to set the Right property equal to.
- method
- MethodInfo
要將 MethodInfo 屬性設定為與之相等的 Method。A MethodInfo to set the Method property equal to.
傳回
BinaryExpression,其 NodeType 屬性等於 Add,且 Left、Right 和 Method 屬性設定為指定的值。A BinaryExpression that has the NodeType property equal to Add and the Left, Right and Method properties set to the specified values.
例外狀況
left
或 right
為 null
。left
or right
is null
.
method
不是 null
,而它所代表的方法會傳回 void
、不是 static
(Visual Basic 中的 Shared
),或者未確切採用兩個引數。method
is not null
and the method it represents returns void
, is not static
(Shared
in Visual Basic), or does not take exactly two arguments.
method
是 null
,而且未定義 left
.Type 和 right
.Type 的加法運算子。method
is null
and the addition operator is not defined for left
.Type and right
.Type.
備註
產生的 BinaryExpression 屬性會 Method 設定為實方法。The resulting BinaryExpression has the Method property set to the implementing method. Type屬性會設定為節點的型別。The Type property is set to the type of the node. 如果節點已提升, IsLifted 和 IsLiftedToNull 屬性都是 true
。If the node is lifted, the IsLifted and IsLiftedToNull properties are both true
. 否則為 false
。Otherwise, they are false
. Conversion 屬性為 null
。The Conversion property is null
.
下列資訊描述實方法、節點型別,以及是否要提升節點。The following information describes the implementing method, the node type, and whether a node is lifted.
實方法Implementing Method
下列規則決定作業的實方法:The following rules determine the implementing method for the operation:
如果不
method
是,null
而它代表非 void,則static
Shared
在採用兩個引數的 Visual Basic) 方法中 (,它就是節點的實作為方法。Ifmethod
is notnull
and it represents a non-void,static
(Shared
in Visual Basic) method that takes two arguments, it is the implementing method for the node.否則,如果 Type 或的屬性代表多載
left
right
加法運算子的使用者定義型別, MethodInfo 表示該方法的會是實方法。Otherwise, if the Type property of eitherleft
orright
represents a user-defined type that overloads the addition operator, the MethodInfo that represents that method is the implementing method.否則為
left
。輸入和right
。類型為數數值型別,實方法為null
。Otherwise, ifleft
.Type andright
.Type are numeric types, the implementing method isnull
.
節點類型和提升與未提升Node Type and Lifted versus Non-Lifted
如果執行中的方法不是 null
:If the implementing method is not null
:
如果為,則為
left
。輸入和right
。類型可指派給實方法的對應引數類型,而不會引發節點。Ifleft
.Type andright
.Type are assignable to the corresponding argument types of the implementing method, the node is not lifted. 節點的型別是實方法的傳回型別。The type of the node is the return type of the implementing method.如果符合下列兩個條件,節點就會被提升,而節點的類型會是可為 null 的型別,該型別會對應至實方法的傳回型別:If the following two conditions are satisfied, the node is lifted and the type of the node is the nullable type that corresponds to the return type of the implementing method:
left
.輸入和right
。類型是數值型別,其中至少有一個可為 null,而對應的不可為 null 的類型等於實方法的對應引數類型。left
.Type andright
.Type are both value types of which at least one is nullable and the corresponding non-nullable types are equal to the corresponding argument types of the implementing method.實方法的傳回型別是不可為 null 的實值型別。The return type of the implementing method is a non-nullable value type.
如果實方法為 null
:If the implementing method is null
:
如果為,則為
left
。輸入和right
。類型不可為 null,節點不會被提升。Ifleft
.Type andright
.Type are both non-nullable, the node is not lifted. 節點的型別是預先定義的加法運算子的結果型別。The type of the node is the result type of the predefined addition operator.如果為,則為
left
。輸入和right
。型別可以是可為 null,而是會提升節點。Ifleft
.Type andright
.Type are both nullable, the node is lifted. 節點的型別是可為 null 的型別,它會對應到預先定義的加法運算子的結果型別。The type of the node is the nullable type that corresponds to the result type of the predefined addition operator.