diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index 6de7050692..a43841d731 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -5690,6 +5690,11 @@ namespace System.Management.Automation.Language return memberInfo.Value; } + if (string.Equals(member, "Length", StringComparison.OrdinalIgnoreCase) || string.Equals(member, "Count", StringComparison.OrdinalIgnoreCase)) + { + return 1; + } + if (context != null && context.IsStrictVersion(2)) { // If the member is undefined and we're in strict mode, throw an exception... diff --git a/test/powershell/engine/ETS/Adapter.Tests.ps1 b/test/powershell/engine/ETS/Adapter.Tests.ps1 index 710182925b..a9ba215973 100644 --- a/test/powershell/engine/ETS/Adapter.Tests.ps1 +++ b/test/powershell/engine/ETS/Adapter.Tests.ps1 @@ -112,6 +112,33 @@ Describe "Adapter Tests" -tags "CI" { Remove-TypeData TestCodeMethodInvokationWithVoidReturn } } + + It "Count and length property works for singletons" { + # Return magic Count and Length property if it absent. + $x = 5 + $x.Count | Should Be 1 + $x.Length | Should Be 1 + + $null.Count | Should Be 0 + $null.Length | Should Be 0 + + (10).Count | Should Be 1 + (10).Length | Should Be 1 + + ("a").Count | Should Be 1 + # The Length property exists for strings, so we skip the test in the context. + # ("a").Length | Should Be 1 + + ([psobject] @{ foo = 'bar' }).Count | Should Be 1 + ([psobject] @{ foo = 'bar' }).Length | Should Be 1 + + ([pscustomobject] @{ foo = 'bar' }).Count | Should Be 1 + ([pscustomobject] @{ foo = 'bar' }).Length | Should Be 1 + + # Return real Count and Length property if it present. + ([pscustomobject] @{ foo = 'bar'; count = 5 }).Count | Should Be 5 + ([pscustomobject] @{ foo = 'bar'; length = 5 }).Length | Should Be 5 + } } Describe "Adapter XML Tests" -tags "CI" {