NewArrayExpression 类

定义

表示创建新数组,并可能初始化新数组的元素。

public ref class NewArrayExpression : System::Linq::Expressions::Expression
public ref class NewArrayExpression sealed : System::Linq::Expressions::Expression
public class NewArrayExpression : System.Linq.Expressions.Expression
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating arrays at runtime requires dynamic code generation.")]
public class NewArrayExpression : System.Linq.Expressions.Expression
public sealed class NewArrayExpression : System.Linq.Expressions.Expression
type NewArrayExpression = class
    inherit Expression
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating arrays at runtime requires dynamic code generation.")>]
type NewArrayExpression = class
    inherit Expression
Public Class NewArrayExpression
Inherits Expression
Public NotInheritable Class NewArrayExpression
Inherits Expression
继承
NewArrayExpression
属性

示例

以下示例创建一个 NewArrayExpression 对象,该对象表示创建和初始化字符串的一维数组。

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 对象,该对象表示创建字符串的二维数组。

// 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)

注解

下表显示了可用于根据所需创建NewArrayExpressionNodeType的不同工厂方法。

NodeType 工厂方法
NewArrayBounds NewArrayBounds
NewArrayInit NewArrayInit

属性

名称 说明
CanReduce

指示节点可以缩减为更简单的节点。 如果返回 true,则可以调用 Reduce()以生成化简形式。

(继承自 Expression)
Expressions

如果NodeType属性值为 NewArrayBounds,则获取数组的边界;如果属性的值为 NodeType,则获取初始化新数组的元素的值NewArrayInit

NodeType

获取此 Expression类型的节点类型。

(继承自 Expression)
Type

获取表示 Expression 的表达式的静态类型。

Type

获取表示 Expression 的表达式的静态类型。

(继承自 Expression)

方法

名称 说明
Accept(ExpressionVisitor)

调度到此节点类型的特定访问方法。 例如,MethodCallExpression调用 .VisitMethodCall(MethodCallExpression)

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Reduce()

将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。

(继承自 Expression)
ReduceAndCheck()

将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。

(继承自 Expression)
ReduceExtensions()

将表达式减少为已知节点类型(不是扩展节点),或者仅返回表达式(如果它已是已知类型)。

(继承自 Expression)
ToString()

返回文本 Expression表示形式。

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

创建类似于此表达式的新表达式,但使用提供的子级。 如果所有子级都相同,它将返回此表达式。

VisitChildren(ExpressionVisitor)

减少节点,然后在减少的表达式上调用访问者委托。 如果节点不可减少,该方法将引发异常。

(继承自 Expression)

适用于