NewArrayExpression 类

定义

表示创建一个新数组,并可能初始化该新数组的元素。Represents creating a new array and possibly initializing the elements of the new array.

public ref class NewArrayExpression : System::Linq::Expressions::Expression
public ref class NewArrayExpression sealed : System::Linq::Expressions::Expression
public class NewArrayExpression : System.Linq.Expressions.Expression
public sealed class NewArrayExpression : System.Linq.Expressions.Expression
type NewArrayExpression = class
    inherit Expression
Public Class NewArrayExpression
Inherits Expression
Public NotInheritable Class NewArrayExpression
Inherits Expression
继承
NewArrayExpression

示例

下面的示例创建一个 NewArrayExpression 对象,该对象表示创建和初始化字符串的一维数组。The following example creates a NewArrayExpression object that represents creating and initializing a one-dimensional array of strings.

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"}

下一个示例创建一个 NewArrayExpression 表示创建一维字符串数组的对象。The next example creates a NewArrayExpression object that represents creating a two-dimensional array of strings.

// Create an expression tree that represents creating a
// two-dimensional array of type string with bounds [3,2].
System.Linq.Expressions.NewArrayExpression newArrayExpression =
    System.Linq.Expressions.Expression.NewArrayBounds(
            typeof(string),
            System.Linq.Expressions.Expression.Constant(3),
            System.Linq.Expressions.Expression.Constant(2));

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

// This code produces the following output:
//
// new System.String[,](3, 2)
' Create an expression tree that represents creating a string
' array with rank 2 and bounds (3,2).
Dim newArrayExpression As System.Linq.Expressions.NewArrayExpression = _
    System.Linq.Expressions.Expression.NewArrayBounds( _
            Type.GetType("System.String"), _
            System.Linq.Expressions.Expression.Constant(3), _
            System.Linq.Expressions.Expression.Constant(2))

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

' This code produces the following output:
'
' new System.String[,](3, 2)

注解

下表显示了不同的工厂方法,你可以根据需要使用这些方法来创建 NewArrayExpression NodeTypeThe following table shows the different factory methods that you can use to create a NewArrayExpression depending on the NodeType you require.

NodeType 工厂方法Factory Methods
NewArrayBounds NewArrayBounds
NewArrayInit NewArrayInit

属性

CanReduce

指示可将节点简化为更简单的节点。Indicates that the node can be reduced to a simpler node. 如果返回 true,则可以调用 Reduce() 以生成简化形式。If this returns true, Reduce() can be called to produce the reduced form.

(继承自 Expression)
Expressions

如果 NodeType 属性的值为 NewArrayBounds,则获取数组的界限;如果 NodeType 属性的值为 NewArrayInit,则获取用来初始化新数组的元素的值。Gets the bounds of the array if the value of the NodeType property is NewArrayBounds, or the values to initialize the elements of the new array if the value of the NodeType property is NewArrayInit.

NodeType

获取此 Expression 的节点类型。Gets the node type of this Expression.

(继承自 Expression)
Type

获取此 Expression 表示的表达式的静态类型。Gets the static type of the expression that this Expression represents.

Type

获取此 Expression 表示的表达式的静态类型。Gets the static type of the expression that this Expression represents.

(继承自 Expression)

方法

Accept(ExpressionVisitor)

调度到此节点类型的特定 Visit 方法。Dispatches to the specific visit method for this node type. 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression).

Accept(ExpressionVisitor)

调度到此节点类型的特定 Visit 方法。Dispatches to the specific visit method for this node type. 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression).

(继承自 Expression)
Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Reduce()

将此节点简化为更简单的表达式。Reduces this node to a simpler expression. 如果 CanReduce 返回 true,则它应返回有效的表达式。If CanReduce returns true, this should return a valid expression. 此方法可以返回本身必须简化的另一个节点。This method can return another node which itself must be reduced.

(继承自 Expression)
ReduceAndCheck()

将此节点简化为更简单的表达式。Reduces this node to a simpler expression. 如果 CanReduce 返回 true,则它应返回有效的表达式。If CanReduce returns true, this should return a valid expression. 此方法可以返回本身必须简化的另一个节点。This method can return another node which itself must be reduced.

(继承自 Expression)
ReduceExtensions()

将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type.

(继承自 Expression)
ToString()

返回 Expression 的的文本化表示形式。Returns a textual representation of the Expression.

(继承自 Expression)
Update(IEnumerable<Expression>)

创建一个新的表达式,它类似于此表达式,但使用所提供的子级。Creates a new expression that is like this one, but using the supplied children. 如果所有子级均相同,它将返回此表达式。If all of the children are the same, it will return this expression.

VisitChildren(ExpressionVisitor)

简化节点,然后对简化的表达式调用访问者委托。Reduces the node and then calls the visitor delegate on the reduced expression. 该方法在节点不可简化时引发异常。The method throws an exception if the node is not reducible.

(继承自 Expression)

适用于