Expression.ConvertChecked 方法

定義

建立代表轉換作業的 UnaryExpression;若目標類型已溢位,此運算式便會擲回例外狀況。Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed.

多載

ConvertChecked(Expression, Type)

建立代表轉換作業的 UnaryExpression;若目標類型已溢位,此運算式便會擲回例外狀況。Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed.

ConvertChecked(Expression, Type, MethodInfo)

建立代表轉換作業的 UnaryExpression,如果目標類型已溢位,且已指定此作業的實作方法,則此作業會擲回例外狀況。Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed and for which the implementing method is specified.

ConvertChecked(Expression, Type)

建立代表轉換作業的 UnaryExpression;若目標類型已溢位,此運算式便會擲回例外狀況。Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed.

public:
 static System::Linq::Expressions::UnaryExpression ^ ConvertChecked(System::Linq::Expressions::Expression ^ expression, Type ^ type);
public static System.Linq.Expressions.UnaryExpression ConvertChecked (System.Linq.Expressions.Expression expression, Type type);
static member ConvertChecked : System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.UnaryExpression
Public Shared Function ConvertChecked (expression As Expression, type As Type) As UnaryExpression

參數

expression
Expression

要將 Expression 屬性設定為與之相等的 OperandAn Expression to set the Operand property equal to.

type
Type

要將 Type 屬性設定為與之相等的 TypeA Type to set the Type property equal to.

傳回

UnaryExpression

UnaryExpression,其 NodeType 屬性等於 ConvertChecked,且 OperandType 屬性設定為指定的值。A UnaryExpression that has the NodeType property equal to ConvertChecked and the Operand and Type properties set to the specified values.

例外狀況

expressiontypenullexpression or type is null.

expression.Type 和 type 之間未定義轉換運算子。No conversion operator is defined between expression.Type and type.

備註

Method產生的屬性 UnaryExpression 會設定為實方法。The Method property of the resulting UnaryExpression is set to the implementing method. IsLiftedToNull 屬性為 falseThe IsLiftedToNull property is false. 如果節點已提升, IsLifted 則為 trueIf the node is lifted, IsLifted is true. 否則,會是 falseOtherwise, it is false.

實方法Implementing Method

下列規則決定作業的實方法:The following rules determine the implementing method for the operation:

  • 如果其中一項 expression 。輸入或 type 是定義隱含或明確轉換運算子的使用者定義型別, MethodInfo 代表該運算子的是實方法。If either expression.Type or type is a user-defined type that defines an implicit or explicit conversion operator, the MethodInfo that represents that operator is the implementing method.

  • 否則:Otherwise:

    • 如果兩者都是 expression 。輸入並 type 代表數值或布林值類型,或可為 null 或不可為 null 的列舉類型,實值為 nullIf both expression.Type and type represent numeric or Boolean types, or nullable or non-nullable enumeration types, the implementing method is null.

    • 如果其中一項 expression 。型別或 type 為參考型別,而且有明確的裝箱、取消裝箱或參考轉換 expression 。型別為 type ,實方法是 nullIf either expression.Type or type is a reference type and an explicit boxing, unboxing, or reference conversion exists from expression.Type to type, the implementing method is null.

提升與未提升Lifted versus Non-Lifted

如果執行中的方法不是 nullIf the implementing method is not null:

  • 如果為,則為 expression 。型別可指派給實方法的引數型別,而實方法的傳回型別可指派給 type ,節點則不會被提升。If expression.Type is assignable to the argument type of the implementing method and the return type of the implementing method is assignable to type, the node is not lifted.

  • 如果其中一個或兩者皆為 expression 。型別或 type 是可為 null 的實值型別,而且對應的不可為 null 實值型別會分別與實方法的引數型別和傳回型別相等,而是會引發節點。If either or both of expression.Type or type are a nullable value type and the corresponding non-nullable value types are equal to the argument type and the return type of the implementing method respectively, the node is lifted.

如果實方法為 nullIf the implementing method is null:

  • 如果兩者都是 expression 。類型且不可 type 為 null,節點不會被提升。If both expression.Type and type are non-nullable, the node is not lifted.

  • 否則,就會提升節點。Otherwise the node is lifted.

適用於

ConvertChecked(Expression, Type, MethodInfo)

建立代表轉換作業的 UnaryExpression,如果目標類型已溢位,且已指定此作業的實作方法,則此作業會擲回例外狀況。Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed and for which the implementing method is specified.

public:
 static System::Linq::Expressions::UnaryExpression ^ ConvertChecked(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::Reflection::MethodInfo ^ method);
public static System.Linq.Expressions.UnaryExpression ConvertChecked (System.Linq.Expressions.Expression expression, Type type, System.Reflection.MethodInfo method);
public static System.Linq.Expressions.UnaryExpression ConvertChecked (System.Linq.Expressions.Expression expression, Type type, System.Reflection.MethodInfo? method);
static member ConvertChecked : System.Linq.Expressions.Expression * Type * System.Reflection.MethodInfo -> System.Linq.Expressions.UnaryExpression
Public Shared Function ConvertChecked (expression As Expression, type As Type, method As MethodInfo) As UnaryExpression

參數

expression
Expression

要將 Expression 屬性設定為與之相等的 OperandAn Expression to set the Operand property equal to.

type
Type

要將 Type 屬性設定為與之相等的 TypeA Type to set the Type property equal to.

method
MethodInfo

要將 MethodInfo 屬性設定為與之相等的 MethodA MethodInfo to set the Method property equal to.

傳回

UnaryExpression

UnaryExpression,其 NodeType 屬性等於 ConvertChecked,且 OperandTypeMethod 屬性設定為指定的值。A UnaryExpression that has the NodeType property equal to ConvertChecked and the Operand, Type, and Method properties set to the specified values.

例外狀況

expressiontypenullexpression or type 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 one argument.

expression.Type 和 type 之間未定義轉換運算子。No conversion operator is defined between expression.Type and type.

-或--or- expression.Type 無法指派給 method 所代表方法的引數類型。expression.Type is not assignable to the argument type of the method represented by method.

-或--or- method 所代表方法的傳回類型無法指派給 typeThe return type of the method represented by method is not assignable to type.

-或--or- expression.Type 或 type 是不可為 Null 的實值類型,且對應之不可為 Null 的實值類型不等於 method 所表示之方法的引數類型或傳回類型。expression.Type or type is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by method.

已找到一個以上的方法符合 method 說明。More than one method that matches the method description was found.

備註

Method產生的屬性 UnaryExpression 會設定為實方法。The Method property of the resulting UnaryExpression is set to the implementing method. IsLiftedToNull 屬性為 falseThe IsLiftedToNull property is false. 如果節點已提升, IsLifted 則為 trueIf the node is lifted, IsLifted is true. 否則,會是 falseOtherwise, it is false.

實方法Implementing Method

下列規則決定作業的實方法:The following rules determine the implementing method for the operation:

  • 如果方法不是 null ,則為實方法。If method is not null, it is the implementing method. 它必須代表 static Shared 採用一個引數的 Visual Basic) 方法中的非 void、 (。It must represent a non-void, static (Shared in Visual Basic) method that takes one argument.

  • 否則,則為 expression 。輸入或 type 是定義隱含或明確轉換運算子的使用者定義型別, MethodInfo 代表該運算子的是實方法。Otherwise, if either expression.Type or type is a user-defined type that defines an implicit or explicit conversion operator, the MethodInfo that represents that operator is the implementing method.

  • 否則:Otherwise:

    • 如果兩者都是 expression 。輸入並 type 代表數值或布林值類型,或可為 null 或不可為 null 的列舉類型,實值為 nullIf both expression.Type and type represent numeric or Boolean types, or nullable or non-nullable enumeration types, the implementing method is null.

    • 如果其中一項 expression 。型別或 type 為參考型別,而且有明確的裝箱、取消裝箱或參考轉換 expression 。型別為 type ,實方法是 nullIf either expression.Type or type is a reference type and an explicit boxing, unboxing, or reference conversion exists from expression.Type to type, the implementing method is null.

提升與未提升Lifted versus Non-Lifted

如果執行中的方法不是 nullIf the implementing method is not null:

  • 如果為,則為 expression 。型別可指派給實方法的引數型別,而實方法的傳回型別可指派給 type ,節點則不會被提升。If expression.Type is assignable to the argument type of the implementing method and the return type of the implementing method is assignable to type, the node is not lifted.

  • 如果其中一個或兩者皆為 expression 。型別或 type 是可為 null 的實值型別,而且對應的不可為 null 實值型別會分別與實方法的引數型別和傳回型別相等,而是會引發節點。If either or both of expression.Type or type are a nullable value type and the corresponding non-nullable value types are equal to the argument type and the return type of the implementing method respectively, the node is lifted.

如果實方法為 nullIf the implementing method is null:

  • 如果兩者都是 expression 。類型且不可 type 為 null,節點不會被提升。If both expression.Type and type are non-nullable, the node is not lifted.

  • 否則,就會提升節點。Otherwise the node is lifted.

適用於