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)

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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

C#
public static System.Linq.Expressions.NewExpression New(System.Reflection.ConstructorInfo constructor);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

New(Type)

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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

C#
public static System.Linq.Expressions.NewExpression New(Type type);

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.

C#
// 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()

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

New(ConstructorInfo, IEnumerable<Expression>)

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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

C#
public static System.Linq.Expressions.NewExpression New(System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
C#
public static System.Linq.Expressions.NewExpression New(System.Reflection.ConstructorInfo constructor, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

New(ConstructorInfo, Expression[])

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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

C#
public static System.Linq.Expressions.NewExpression New(System.Reflection.ConstructorInfo constructor, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.NewExpression New(System.Reflection.ConstructorInfo constructor, params System.Linq.Expressions.Expression[]? arguments);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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

C#
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);
C#
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);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
NewExpression.cs
Source:
NewExpression.cs
Source:
NewExpression.cs

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.

C#
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);
C#
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);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0