Expression.New Method

Definition

Creates a NewExpression.

Overloads

New(ConstructorInfo)

Creates a NewExpression that represents calling the specified constructor that takes no arguments.

New(Type)

Creates a NewExpression that represents calling the parameterless constructor of the specified type.

New(ConstructorInfo, IEnumerable<Expression>)

Creates a NewExpression that represents calling the specified constructor with the specified arguments.

New(ConstructorInfo, Expression[])

Creates a NewExpression that represents calling the specified constructor with the specified arguments.

New(ConstructorInfo, IEnumerable<Expression>, IEnumerable<MemberInfo>)

Creates a NewExpression that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified.

New(ConstructorInfo, IEnumerable<Expression>, MemberInfo[])

Creates a NewExpression that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified as an array.

New(ConstructorInfo)

Creates a NewExpression that represents calling the specified constructor that takes no arguments.

public:
 static System::Linq::Expressions::NewExpression ^ New(System::Reflection::ConstructorInfo ^ constructor);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor);
static member New : System.Reflection.ConstructorInfo -> System.Linq.Expressions.NewExpression
Public Shared Function New (constructor As ConstructorInfo) As NewExpression

Parameters

constructor
ConstructorInfo

The ConstructorInfo to set the Constructor property equal to.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor property set to the specified value.

Exceptions

constructor is null.

The constructor that constructor represents has at least one parameter.

Remarks

The Arguments and Members properties of the resulting NewExpression are empty collections. The Type property represents the declaring type of the constructor represented by constructor.

Applies to

New(Type)

Creates a NewExpression that represents calling the parameterless constructor of the specified type.

public:
 static System::Linq::Expressions::NewExpression ^ New(Type ^ type);
public static System.Linq.Expressions.NewExpression New (Type type);
static member New : Type -> System.Linq.Expressions.NewExpression
Public Shared Function New (type As Type) As NewExpression

Parameters

type
Type

A Type that has a constructor that takes no arguments.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor property set to the ConstructorInfo that represents the constructor without parameters for the specified type.

Exceptions

type is null.

The type that type represents does not have a constructor without parameters.

Examples

The following example demonstrates how to use the New(Type) method to create a NewExpression that represents constructing a new instance of a dictionary object by calling the constructor without parameters.

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

Console.WriteLine(newDictionaryExpression.ToString());

// This code produces the following output:
//
// new Dictionary`2()
' 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]"))

Console.WriteLine(newDictionaryExpression.ToString())

' This code produces the following output:
'
' new Dictionary`2()

Remarks

The type parameter must represent a type that has a constructor without parameters.

The Arguments and Members properties of the resulting NewExpression are empty collections. The Type property is equal to type.

Applies to

New(ConstructorInfo, IEnumerable<Expression>)

Creates a NewExpression that represents calling the specified constructor with the specified arguments.

public:
 static System::Linq::Expressions::NewExpression ^ New(System::Reflection::ConstructorInfo ^ constructor, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);
static member New : System.Reflection.ConstructorInfo * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.NewExpression
Public Shared Function New (constructor As ConstructorInfo, arguments As IEnumerable(Of Expression)) As NewExpression

Parameters

constructor
ConstructorInfo

The ConstructorInfo to set the Constructor property equal to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Arguments collection.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor and Arguments properties set to the specified values.

Exceptions

constructor is null.

-or-

An element of arguments is null.

The arguments parameter does not contain the same number of elements as the number of parameters for the constructor that constructor represents.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the constructor that constructor represents.

Remarks

The arguments parameter must contain the same number of elements as the number of parameters for the constructor represented by constructor. If arguments is null, it is considered empty, and the Arguments property of the resulting NewExpression is an empty collection.

The Type property of the resulting NewExpression represents the declaring type of the constructor represented by constructor. The Members property is an empty collection.

Applies to

New(ConstructorInfo, Expression[])

Creates a NewExpression that represents calling the specified constructor with the specified arguments.

public:
 static System::Linq::Expressions::NewExpression ^ New(System::Reflection::ConstructorInfo ^ constructor, ... cli::array <System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, params System.Linq.Expressions.Expression[] arguments);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, params System.Linq.Expressions.Expression[]? arguments);
static member New : System.Reflection.ConstructorInfo * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.NewExpression
Public Shared Function New (constructor As ConstructorInfo, ParamArray arguments As Expression()) As NewExpression

Parameters

constructor
ConstructorInfo

The ConstructorInfo to set the Constructor property equal to.

arguments
Expression[]

An array of Expression objects to use to populate the Arguments collection.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor and Arguments properties set to the specified values.

Exceptions

constructor is null.

-or-

An element of arguments is null.

The length of arguments does match the number of parameters for the constructor that constructor represents.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the constructor that constructor represents.

Remarks

The arguments parameter must contain the same number of elements as the number of parameters for the constructor represented by constructor. If arguments is null, it is considered empty, and the Arguments property of the resulting NewExpression is an empty collection.

The Type property of the resulting NewExpression represents the declaring type of the constructor represented by constructor. The Members property is an empty collection.

Applies to

New(ConstructorInfo, IEnumerable<Expression>, IEnumerable<MemberInfo>)

Creates a NewExpression that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified.

public:
 static System::Linq::Expressions::NewExpression ^ New(System::Reflection::ConstructorInfo ^ constructor, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ arguments, System::Collections::Generic::IEnumerable<System::Reflection::MemberInfo ^> ^ members);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments, System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo> members);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments, System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>? members);
static member New : System.Reflection.ConstructorInfo * seq<System.Linq.Expressions.Expression> * seq<System.Reflection.MemberInfo> -> System.Linq.Expressions.NewExpression
Public Shared Function New (constructor As ConstructorInfo, arguments As IEnumerable(Of Expression), members As IEnumerable(Of MemberInfo)) As NewExpression

Parameters

constructor
ConstructorInfo

The ConstructorInfo to set the Constructor property equal to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Arguments collection.

members
IEnumerable<MemberInfo>

An IEnumerable<T> that contains MemberInfo objects to use to populate the Members collection.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor, Arguments and Members properties set to the specified values.

Exceptions

constructor is null.

-or-

An element of arguments is null.

-or-

An element of members is null.

The arguments parameter does not contain the same number of elements as the number of parameters for the constructor that constructor represents.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the constructor that constructor represents.

-or-

The members parameter does not have the same number of elements as arguments.

-or-

An element of arguments has a Type property that represents a type that is not assignable to the type of the member that is represented by the corresponding element of members.

Remarks

The arguments parameter must contain the same number of elements as the number of parameters for the constructor represented by constructor. If arguments is null, it is considered empty, and the Arguments property of the resulting NewExpression is an empty collection.

If members is null, the Members property of the resulting NewExpression is an empty collection. If members is not null, it must have the same number of elements as arguments and each element must not be null. Each element of members must be a PropertyInfo, FieldInfo or MethodInfo that represents an instance member on the declaring type of the constructor represented by constructor. If it represents a property, the property must have a get accessor. The corresponding element of arguments for each element of members must have a Type property that represents a type that is assignable to the type of the member that the members element represents.

The Type property of the resulting NewExpression represents the declaring type of the constructor that constructor represents.

Applies to

New(ConstructorInfo, IEnumerable<Expression>, MemberInfo[])

Creates a NewExpression that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified as an array.

public:
 static System::Linq::Expressions::NewExpression ^ New(System::Reflection::ConstructorInfo ^ constructor, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ arguments, ... cli::array <System::Reflection::MemberInfo ^> ^ members);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments, params System.Reflection.MemberInfo[] members);
public static System.Linq.Expressions.NewExpression New (System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments, params System.Reflection.MemberInfo[]? members);
static member New : System.Reflection.ConstructorInfo * seq<System.Linq.Expressions.Expression> * System.Reflection.MemberInfo[] -> System.Linq.Expressions.NewExpression
Public Shared Function New (constructor As ConstructorInfo, arguments As IEnumerable(Of Expression), ParamArray members As MemberInfo()) As NewExpression

Parameters

constructor
ConstructorInfo

The ConstructorInfo to set the Constructor property equal to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Arguments collection.

members
MemberInfo[]

An array of MemberInfo objects to use to populate the Members collection.

Returns

A NewExpression that has the NodeType property equal to New and the Constructor, Arguments and Members properties set to the specified values.

Exceptions

constructor is null.

-or-

An element of arguments is null.

-or-

An element of members is null.

The arguments parameter does not contain the same number of elements as the number of parameters for the constructor that constructor represents.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the constructor that constructor represents.

-or-

The members parameter does not have the same number of elements as arguments.

-or-

An element of arguments has a Type property that represents a type that is not assignable to the type of the member that is represented by the corresponding element of members.

Remarks

The arguments parameter must contain the same number of elements as the number of parameters for the constructor represented by constructor. If arguments is null, it is considered empty, and the Arguments property of the resulting NewExpression is an empty collection.

If members is null, the Members property of the resulting NewExpression is an empty collection. If members is not null, it must have the same number of elements as arguments and each element must not be null. Each element of members must be a PropertyInfo, FieldInfo or MethodInfo that represents an instance member on the declaring type of the constructor represented by constructor. If it represents a property, the property must be able to retrieve the value of the associated field. The corresponding element of arguments for each element of members must have a Type property that represents a type that is assignable to the type of the member that the members element represents.

The Type property of the resulting NewExpression represents the declaring type of the constructor that constructor represents.

Applies to