/// <summary>
/// Creates a <see cref="NewExpression"/> that represents calling the parameterless constructor of the specified type.
/// </summary>
/// <param name="type">A <see cref="Type"/> that has a constructor that takes no arguments. </param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to New and the Constructor property set to the ConstructorInfo that represents the parameterless constructor of the specified type.</returns>
public static NewExpression New(Type type)
{
ContractUtils.RequiresNotNull(type, "type");
if (type == typeof(void))
{
throw Error.ArgumentCannotBeOfTypeVoid();
}
if (!type.IsValueType)
{
var constructorInfo = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(c => c.GetParameters().Length == 0);
if (constructorInfo == null)
{
throw Error.TypeMissingDefaultConstructor(type);
}
return(New(constructorInfo));
}
return(new NewValueTypeExpression(type, EmptyCollection <Expression> .Instance, null));
}