Skip to content

Latest commit

 

History

History
31 lines (17 loc) · 909 Bytes

README.md

File metadata and controls

31 lines (17 loc) · 909 Bytes

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