Make LanguagePrimitive.GetEnumerable treat 'DataTable' as Enumerable (#6511)
* Make LanguagePrimitive.GetEnumerable treat 'DataTable' as Enumerable
This commit is contained in:
parent
ec0f1116db
commit
cd075ea4b4
|
@ -476,12 +476,10 @@ namespace System.Management.Automation
|
|||
return null;
|
||||
}
|
||||
|
||||
#if !CORECLR
|
||||
private static IEnumerable DataTableEnumerable(object obj)
|
||||
{
|
||||
return (((DataTable)obj).Rows);
|
||||
}
|
||||
#endif
|
||||
|
||||
private static IEnumerable TypicalEnumerable(object obj)
|
||||
{
|
||||
|
@ -517,12 +515,10 @@ namespace System.Management.Automation
|
|||
|
||||
private static GetEnumerableDelegate CalculateGetEnumerable(Type objectType)
|
||||
{
|
||||
#if !CORECLR
|
||||
if (typeof(DataTable).IsAssignableFrom(objectType))
|
||||
{
|
||||
return LanguagePrimitives.DataTableEnumerable;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Don't treat IDictionary or XmlNode as enumerable...
|
||||
if (typeof(IEnumerable).IsAssignableFrom(objectType)
|
||||
|
|
|
@ -32,4 +32,18 @@ Describe "Language Primitive Tests" -Tags "CI" {
|
|||
$a[0] = $a = [PSObject](,1)
|
||||
[System.Management.Automation.LanguagePrimitives]::IsTrue($a) | Should -BeTrue
|
||||
}
|
||||
|
||||
It "LanguagePrimitives.GetEnumerable should treat 'DataTable' as Enumerable" {
|
||||
$dt = [System.Data.DataTable]::new("test")
|
||||
$dt.Columns.Add("Name", [string]) > $null
|
||||
$dt.Columns.Add("Age", [string]) > $null
|
||||
$dr = $dt.NewRow(); $dr["Name"] = "John"; $dr["Age"] = "20"
|
||||
$dr2 = $dt.NewRow(); $dr["Name"] = "Susan"; $dr["Age"] = "25"
|
||||
$dt.Rows.Add($dr); $dt.Rows.Add($dr2)
|
||||
|
||||
[System.Management.Automation.LanguagePrimitives]::IsObjectEnumerable($dt) | Should -BeTrue
|
||||
$count = 0
|
||||
[System.Management.Automation.LanguagePrimitives]::GetEnumerable($dt) | ForEach-Object { $count++ }
|
||||
$count | Should -Be 2
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue