Expression.NewArrayInit 方法

定義

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

多載

NewArrayInit(Type, IEnumerable<Expression>)

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

NewArrayInit(Type, Expression[])

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

NewArrayInit(Type, IEnumerable<Expression>)

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

public:
 static System::Linq::Expressions::NewArrayExpression ^ NewArrayInit(Type ^ type, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.NewArrayExpression NewArrayInit (Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
static member NewArrayInit : Type * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.NewArrayExpression
Public Shared Function NewArrayInit (type As Type, initializers As IEnumerable(Of Expression)) As NewArrayExpression

參數

type
Type

Type,代表陣列的項目類型。A Type that represents the element type of the array.

initializers
IEnumerable<Expression>

IEnumerable<T>,其中包含用以填入 Expression 集合的 Expressions 物件。An IEnumerable<T> that contains Expression objects to use to populate the Expressions collection.

傳回

NewArrayExpression

NewArrayExpression,其 NodeType 屬性等於 NewArrayInit,而 Expressions 屬性設定為指定的值。A NewArrayExpression that has the NodeType property equal to NewArrayInit and the Expressions property set to the specified value.

例外狀況

typeinitializersnulltype or initializers is null.

-或--or- initializers 的項目是 nullAn element of initializers is null.

initializers 之項目的 Type 屬性所表示的類型無法指派給 type 所表示的類型。The Type property of an element of initializers represents a type that is not assignable to the type that type represents.

範例

下列範例示範如何使用 NewArrayInit 方法來建立運算式樹狀架構,此樹狀結構表示建立以字串運算式清單初始化的一維字串陣列。The following example demonstrates how to use the NewArrayInit method to create an expression tree that represents creating a one-dimensional string array that is initialized with a list of string expressions.

List<System.Linq.Expressions.Expression> trees =
    new List<System.Linq.Expressions.Expression>()
        { System.Linq.Expressions.Expression.Constant("oak"),
          System.Linq.Expressions.Expression.Constant("fir"),
          System.Linq.Expressions.Expression.Constant("spruce"),
          System.Linq.Expressions.Expression.Constant("alder") };

// Create an expression tree that represents creating and
// initializing a one-dimensional array of type string.
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayInit(typeof(string), trees);

// Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString());

// This code produces the following output:
//
// new [] {"oak", "fir", "spruce", "alder"}
Dim trees As New System.Collections.Generic.List(Of System.Linq.Expressions.Expression) _
        (New System.Linq.Expressions.Expression() _
         {System.Linq.Expressions.Expression.Constant("oak"), _
          System.Linq.Expressions.Expression.Constant("fir"), _
          System.Linq.Expressions.Expression.Constant("spruce"), _
          System.Linq.Expressions.Expression.Constant("alder")})

' Create an expression tree that represents creating and  
' initializing a one-dimensional array of type string.
Dim newArrayExpression As System.Linq.Expressions.NewArrayExpression = _
    System.Linq.Expressions.Expression.NewArrayInit(Type.GetType("System.String"), trees)

' Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString())

' This code produces the following output:
'
' new [] {"oak", "fir", "spruce", "alder"}

備註

Type 每個專案的屬性 initializers 必須代表可指派給所表示之型別的型別 type ,可能會在 加上引號 之後。The Type property of each element of initializers must represent a type that is assignable to the type represented by type, possibly after it is quoted.

注意

只有當為時,才會將元素加上引號 type ExpressionAn element will be quoted only if type is Expression. 引號表示元素會包裝在節點中 QuoteQuoting means the element is wrapped in a Quote node. 產生的節點是, UnaryExpressionOperand 屬性為的元素 initializersThe resulting node is a UnaryExpression whose Operand property is the element of initializers.

Type產生的屬性 NewArrayExpression 代表其等級為1且其元素類型為的陣列類型 typeThe Type property of the resulting NewArrayExpression represents an array type whose rank is 1 and whose element type is type.

適用於

NewArrayInit(Type, Expression[])

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。Creates a NewArrayExpression that represents creating a one-dimensional array and initializing it from a list of elements.

public:
 static System::Linq::Expressions::NewArrayExpression ^ NewArrayInit(Type ^ type, ... cli::array <System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.NewArrayExpression NewArrayInit (Type type, params System.Linq.Expressions.Expression[] initializers);
static member NewArrayInit : Type * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.NewArrayExpression
Public Shared Function NewArrayInit (type As Type, ParamArray initializers As Expression()) As NewArrayExpression

參數

type
Type

Type,代表陣列的項目類型。A Type that represents the element type of the array.

initializers
Expression[]

用以填入 Expression 集合之 Expressions 物件的陣列。An array of Expression objects to use to populate the Expressions collection.

傳回

NewArrayExpression

NewArrayExpression,其 NodeType 屬性等於 NewArrayInit,而 Expressions 屬性設定為指定的值。A NewArrayExpression that has the NodeType property equal to NewArrayInit and the Expressions property set to the specified value.

例外狀況

typeinitializersnulltype or initializers is null.

-或--or- initializers 的項目是 nullAn element of initializers is null.

initializers 之項目的 Type 屬性所表示的類型無法指派給 type 類型。The Type property of an element of initializers represents a type that is not assignable to the type type.

範例

下列範例示範如何使用 NewArrayInit 方法來建立運算式樹狀架構,此樹狀結構表示建立以字串運算式清單初始化的一維字串陣列。The following example demonstrates how to use the NewArrayInit method to create an expression tree that represents creating a one-dimensional string array that is initialized with a list of string expressions.

List<System.Linq.Expressions.Expression> trees =
    new List<System.Linq.Expressions.Expression>()
        { System.Linq.Expressions.Expression.Constant("oak"),
          System.Linq.Expressions.Expression.Constant("fir"),
          System.Linq.Expressions.Expression.Constant("spruce"),
          System.Linq.Expressions.Expression.Constant("alder") };

// Create an expression tree that represents creating and
// initializing a one-dimensional array of type string.
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayInit(typeof(string), trees);

// Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString());

// This code produces the following output:
//
// new [] {"oak", "fir", "spruce", "alder"}
Dim trees As New System.Collections.Generic.List(Of System.Linq.Expressions.Expression) _
        (New System.Linq.Expressions.Expression() _
         {System.Linq.Expressions.Expression.Constant("oak"), _
          System.Linq.Expressions.Expression.Constant("fir"), _
          System.Linq.Expressions.Expression.Constant("spruce"), _
          System.Linq.Expressions.Expression.Constant("alder")})

' Create an expression tree that represents creating and  
' initializing a one-dimensional array of type string.
Dim newArrayExpression As System.Linq.Expressions.NewArrayExpression = _
    System.Linq.Expressions.Expression.NewArrayInit(Type.GetType("System.String"), trees)

' Output the string representation of the Expression.
Console.WriteLine(newArrayExpression.ToString())

' This code produces the following output:
'
' new [] {"oak", "fir", "spruce", "alder"}

備註

Type 每個專案的屬性 initializers 必須代表可指派給所表示之型別的型別 type ,可能會在 加上引號 之後。The Type property of each element of initializers must represent a type that is assignable to the type represented by type, possibly after it is quoted.

注意

只有當為時,才會將元素加上引號 type ExpressionAn element will be quoted only if type is Expression. 引號表示元素會包裝在節點中 QuoteQuoting means the element is wrapped in a Quote node. 產生的節點是, UnaryExpressionOperand 屬性為的元素 initializersThe resulting node is a UnaryExpression whose Operand property is the element of initializers.

Type產生的屬性 NewArrayExpression 代表其等級為1且其元素類型為的陣列類型 typeThe Type property of the resulting NewArrayExpression represents an array type whose rank is 1 and whose element type is type.

適用於