Skip to content

Commit 51b2a00

Browse files
committed
Add Count and Length property to [pscustomobject]
Add new tests
1 parent 79ae396 commit 51b2a00

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/System.Management.Automation/engine/runtime/Binding/Binders.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5690,6 +5690,11 @@ internal static object GetAdaptedValue(object obj, string member)
56905690
return memberInfo.Value;
56915691
}
56925692

5693+
if (string.Equals(member, "Length", StringComparison.OrdinalIgnoreCase) || string.Equals(member, "Count", StringComparison.OrdinalIgnoreCase))
5694+
{
5695+
return 1;
5696+
}
5697+
56935698
if (context != null && context.IsStrictVersion(2))
56945699
{
56955700
// If the member is undefined and we're in strict mode, throw an exception...

test/powershell/engine/ETS/Adapter.Tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ Describe "Adapter Tests" -tags "CI" {
112112
Remove-TypeData TestCodeMethodInvokationWithVoidReturn
113113
}
114114
}
115+
116+
It "Count and length property works for singletons" {
117+
$x = 5
118+
$x.Count | Should Be 1
119+
$x.Length | Should Be 1
120+
121+
$null.Count | Should Be 0
122+
$null.Length | Should Be 0
123+
124+
(10).Count | Should Be 1
125+
(10).Length | Should Be 1
126+
127+
("a").Count | Should Be 1
128+
("a").Length | Should Be 1
129+
130+
([psobject] @{ foo = 'bar' }).Count | Should Be 1
131+
([psobject] @{ foo = 'bar' }).Length | Should Be 1
132+
133+
([pscustomobject] @{ foo = 'bar' }).Count | Should Be 1
134+
([pscustomobject] @{ foo = 'bar' }).Length | Should Be 1
135+
}
115136
}
116137

117138
Describe "Adapter XML Tests" -tags "CI" {

0 commit comments

Comments
 (0)