Expression.NewArrayInit 方法

定義

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。

多載

NewArrayInit(Type, IEnumerable<Expression>)

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。

NewArrayInit(Type, Expression[])

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。

NewArrayInit(Type, IEnumerable<Expression>)

來源:
NewArrayExpression.cs
來源:
NewArrayExpression.cs
來源:
NewArrayExpression.cs

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。

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,代表陣列的項目類型。

initializers
IEnumerable<Expression>

IEnumerable<T>,其中包含用以填入 Expression 集合的 Expressions 物件。

傳回

NewArrayExpression,其 NodeType 屬性等於 NewArrayInit,而 Expressions 屬性設定為指定的值。

例外狀況

typeinitializersnull

-或-

initializers 的項目是 null

initializers 之項目的 Type 屬性所表示的類型無法指派給 type 所表示的類型。

範例

下列範例示範如何使用 NewArrayInit 方法來建立運算式樹狀結構,代表建立以字串運算式清單初始化的一維字串陣列。

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 表示之型別的類型,可能是在加上引號之後。

注意

只有在 為 Expressiontype ,才會加上引號。 引號表示元素會包裝在節點中 Quote 。 產生的節點是 , UnaryExpressionOperand 屬性是 的 initializers 元素。

Type產生的 NewArrayExpression 屬性代表排名為 1 且其專案類型為 type 的陣列類型。

適用於

NewArrayInit(Type, Expression[])

來源:
NewArrayExpression.cs
來源:
NewArrayExpression.cs
來源:
NewArrayExpression.cs

建立 NewArrayExpression,其代表建立一維陣列,並從項目清單將此陣列初始化。

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,代表陣列的項目類型。

initializers
Expression[]

用以填入 Expression 集合之 Expressions 物件的陣列。

傳回

NewArrayExpression,其 NodeType 屬性等於 NewArrayInit,而 Expressions 屬性設定為指定的值。

例外狀況

typeinitializersnull

-或-

initializers 的項目是 null

initializers 之項目的 Type 屬性所表示的類型無法指派給 type 類型。

範例

下列範例示範如何使用 NewArrayInit 方法來建立運算式樹狀結構,代表建立以字串運算式清單初始化的一維字串陣列。

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 表示之型別的類型,可能是在加上引號之後。

注意

只有在 為 Expressiontype ,才會加上引號。 引號表示元素會包裝在節點中 Quote 。 產生的節點是 , UnaryExpressionOperand 屬性是 的 initializers 元素。

Type產生的 NewArrayExpression 屬性代表排名為 1 且其專案類型為 type 的陣列類型。

適用於