Monday, September 16, 2013

Random notes on [System.Collections...]


These are some very random notes on [System.Collections...]  7:07 PM 9/16/2013
Most of us know you can create a hashtable from syntax like this:

$ps=foreach ($i in $(ps)) {@{$i.id=$i.name}}
$ps | gm -s

   TypeName: System.Collections.Hashtable

But did you know you can use the same syntax to create a SortedList?

$ps=foreach ($i in $(ps)){[System.Collections.SortedList]@{$i.id=$i.name}}

$ps | gm -s

   TypeName: System.Collections.SortedList

'SortedList' is one of some number of data collections found in System.Collections. It creates a sorted list of values based on a unique key. Once loaded 'SortedList' has a faster retrieval speed than other members of System.Collections. Here's an example:

rv -ea 0 SortedNames; rv -ea 0 SortedSDDL
measure-command {
$SortedNames = New-Object System.Collections.SortedList
$SortedSDDL = New-Object System.Collections.SortedList
$index=0
foreach ($i in $(ls)) { $indx = $index++; $SortedNames.Add($indx,$i.name); $SortedSDDL.Add($indx,$i.getaccesscontrol().SDDL)  }
}

PS C:\ps1> rv -ea 0 SortedNames; rv -ea 0 SortedSDDL
PS C:\ps1> measure-command {
>> $SortedNames = New-Object System.Collections.SortedList
>> $SortedSDDL = New-Object System.Collections.SortedList
>> $index=0
>> foreach ($i in $(ls)) { $indx = $index++; $SortedNames.Add($indx,$i.name); $SortedSDDL.Add($indx,$i.getaccesscontrol().SDDL)  }
>> }

PS C:\ps1> $SortedNames.count
935

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 820
Ticks             : 8200868
TotalDays         : 9.49174537037037E-06
TotalHours        : 0.000227801888888889
TotalMinutes      : 0.0136681133333333
TotalSeconds      : 0.8200868
TotalMilliseconds : 820.0868

The following trick I picked up from Powershell.com : http://powershell.com/cs/blogs/tips/archive/2013/09/11/adding-new-type-accelerators-in-powershell.aspx . You can enumerate "Type Accelerators" in Powershell 3.0 with:

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get |Sort-Object -Property Value 

The property 'ImplementedInterfaces' allows you to view the interfaces for various Collections. Some Collections have more interfaces than others:

$PSAGet=[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get
$PSAhashtable = ($PSAGet).hashtable

($PSAhashtable).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IDictionary
True     False    ICollection
True     False    IEnumerable
True     False    ISerializable
True     False    IDeserializationCallback
True     False    ICloneable

$PSAArray = ($PSAGet).array
PS C:\> ($PSAArray).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ICloneable
True     False    IList
True     False    ICollection
True     False    IEnumerable
True     False    IStructuralComparable
True     False    IStructuralEquatable

Unlike [hashtable] and [array], SortedList, ArrayList, IDictionary, IList will need to be added to Powershell Type Accelerator list in order to be used as [SortedList], [ArrayList], etc.


[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('SortedList', [System.Collections.SortedList])
PS C:\> [SortedList]

PS C:\> $PSASortedList =($PSAGet).SortedList
PS C:\> ($PSASortedList).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IDictionary
True     False    ICollection
True     False    IEnumerable
True     False    ICloneable

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('ArrayList', [System.Collections.ArrayList])
PS C:\> [ArrayList]

PS C:\> ([ArrayList]).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IList
True     False    ICollection
True     False    IEnumerable
True     False    ICloneable

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('CBase', [System.Collections.CollectionBase])
[CBase]

PS C:\> ([CBase]).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    IList
True     False    ICollection
True     False    IEnumerable

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('IDictionary', [System.Collections.IDictionary])
([IDictionary]).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ICollection
True     False    IEnumerable

[PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Add('IList', [System.Collections.IList])
([IList]).ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ICollection
True     False    IEnumerable

No comments: