Skip to content

Latest commit

 

History

History

v5-Method-New

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

v5 method new()

PowerShell v5 comes with a new feature. Instead of old fashioned

New-Object System.Text.StringBuilder

it is possible to use the new syntax

[System.Text.StringBuilder]::new()

The new syntax introduces an issue. If a class has a static method New then it cannot be called directly. PowerShell treats it as the constructor, not the static method.

This code works in PowerShell v4 (Test-1.ps1)

[System.Linq.Expressions.Expression]::New([psobject])

It calls the static method New and creates an expression.

In v5 it fails

Cannot find an overload for "new" and the argument count: "1".

The workaround (Test-2.ps1)

[System.Linq.Expressions.Expression]::New.Invoke([psobject])