ListInitExpression 类

定义

表示具有集合初始值设定项的构造函数调用。

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

示例

以下示例创建一个 ListInitExpression 表示具有两个键值对的新字典实例的初始化。

string tree1 = "maple";
string tree2 = "oak";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create two ElementInit objects that represent the
// two key-value pairs to add to the Dictionary.
System.Linq.Expressions.ElementInit elementInit1 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree1.Length),
        System.Linq.Expressions.Expression.Constant(tree1));
System.Linq.Expressions.ElementInit elementInit2 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree2.Length),
        System.Linq.Expressions.Expression.Constant(tree2));

// Create a NewExpression that represents constructing
// a new instance of Dictionary<int, string>.
System.Linq.Expressions.NewExpression newDictionaryExpression =
    System.Linq.Expressions.Expression.New(typeof(Dictionary<int, string>));

// Create a ListInitExpression that represents initializing
// a new Dictionary<> instance with two key-value pairs.
System.Linq.Expressions.ListInitExpression listInitExpression =
    System.Linq.Expressions.Expression.ListInit(
        newDictionaryExpression,
        elementInit1,
        elementInit2);

Console.WriteLine(listInitExpression.ToString());

// This code produces the following output:
//
// new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
// Void Add(Int32, System.String)(3,"oak")}
Dim tree1 As String = "maple"
Dim tree2 As String = "oak"

Dim addMethod As System.Reflection.MethodInfo = _
    Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]").GetMethod("Add")

' Create two ElementInit objects that represent the
' two key-value pairs to add to the Dictionary.
Dim elementInit1 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree1.Length), _
        System.Linq.Expressions.Expression.Constant(tree1))
Dim elementInit2 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree2.Length), _
        System.Linq.Expressions.Expression.Constant(tree2))

' Create a NewExpression that represents constructing
' a new instance of Dictionary(Of Integer, String).
Dim newDictionaryExpression As System.Linq.Expressions.NewExpression = _
    System.Linq.Expressions.Expression.[New](Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]"))

' Create a ListInitExpression that represents initializing
' a new Dictionary(Of T) instance with two key-value pairs.
Dim listInitExpression As System.Linq.Expressions.ListInitExpression = _
    System.Linq.Expressions.Expression.ListInit( _
        newDictionaryExpression, _
        elementInit1, _
        elementInit2)

Console.WriteLine(listInitExpression.ToString())

' This code produces the following output:
'
' new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
' Void Add(Int32, System.String)(3,"oak")

注解

ListInit使用工厂方法创建一个 ListInitExpression

NodeType ListInitExpression属性值为 ListInit.

属性

CanReduce

获取一个值,该值指示是否可以减小此表达式树节点。

CanReduce

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

(继承自 Expression)
Initializers

获取用于初始化集合的元素初始值设定项。

NewExpression

获取包含对集合类型的构造函数的调用的表达式。

NodeType

返回此 Expression 的节点类型。

NodeType

获取此 Expression 的节点类型。

(继承自 Expression)
Type

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

Type

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

(继承自 Expression)

方法

Accept(ExpressionVisitor)

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

(继承自 Expression)
Equals(Object)

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

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Reduce()

将二进制表达式节点简化为更简单的表达式。

Reduce()

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

(继承自 Expression)
ReduceAndCheck()

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

(继承自 Expression)
ReduceExtensions()

将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。

(继承自 Expression)
ToString()

返回 Expression 的的文本化表示形式。

(继承自 Expression)
Update(NewExpression, IEnumerable<ElementInit>)

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

VisitChildren(ExpressionVisitor)

简化节点,然后对简化的表达式调用访问者委托。 该方法在节点不可简化时引发异常。

(继承自 Expression)

适用于