Udostępnij za pośrednictwem


Expression.New Method (Type)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function New ( _
    type As Type _
) As NewExpression
public static NewExpression New(
    Type type
)

Parameters

  • type
    Type: System.Type
    A Type that has a constructor that takes no arguments.

Return Value

Type: System.Linq.Expressions.NewExpression
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

Exception Condition
ArgumentNullException

type is nulla null reference (Nothing in Visual Basic).

ArgumentException

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

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.

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(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]"))

outputBlock.Text &= newDictionaryExpression.ToString() & vbCrLf

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

outputBlock.Text += newDictionaryExpression.ToString() + "\n";

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.