PowerShell Remoting Project Home

Sunday, July 02, 2006

Naive and Generic Object Collections in Powershell

I believed that you must have dealt with collections in Powershell Scriting. PowerShell has a nice way to support naive object array (object[]):

>$a = 1,2,3   #comma were interpreted as object[]
>$a.gettype().AssemblyQualifiedName
System.Object[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

>$a= 1..10   # .. were interpreted as continuous filled object[]
>$a.gettype().AssemblyQualifiedName
System.Object[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
For naive object ArrayList/stack/Queue, you have to use new-object cmdlet
> $a = new-object System.Collections.Stack
> $a.GetType().AssemblyQualifiedName
System.Collections.Stack, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKey
Token=b77a5c561934e089
> $a.Push($([System.Net.IPAddress] "192.168.0.1"))
Most of time, you are fine with naive object model because PowerShell will do the type conversion for you.
>$a= 1..10
>$a |gm
   TypeName: System.Int32

Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     System.Int32 CompareTo(Int32 value), System.Int32 Com...
Equals      Method     System.Boolean Equals(Object obj), System.Boolean Equ...
GetHashCode Method     System.Int32 GetHashCode()
GetType     Method     System.Type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     System.String ToString(), System.String ToString(IFor...
But sometimes it is not good enough. You probably want to use generic object model.

For generic array (like System.Net.IPAddress[]), it requires a conversion:
>$a = ($([System.Net.IPAddress] "192.168.0.1"),$([System.Net.IPAddress] "127.0.0.1") )
>$a.gettype().AssemblyQualifiedName
System.Object[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
>$b= [System.Net.IPAddress[]] $a
> $b.GetType().AssemblyQualifiedName
System.Net.IPAddress[], System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
For generic collection (like System.Collections.ObjectModel.Collection<System.Net.IPAddress>), it becomes a little nasty (it requires Assembly Qualified Name) :
>$a = New-Object System.Collections.ObjectModel.Collection"`1[[System.Net.IPAddress, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
> $a.GetType().AssemblyQualifiedName
System.Collections.ObjectModel.Collection`1[[System.Net.IPAddress, System, Vers
ion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Vers
ion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
> $a.Add($([System.Net.IPAddress] "192.168.0.1"))
What about generic Stack?
> $a = New-Object System.Collections.Generic.Stack"`1[[System.Net.IPAddres
s, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
> $a.GetType().AssemblyQualifiedName
System.Collections.Generic.Stack`1[[System.Net.IPAddress, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
> $a.Push($([System.Net.IPAddress] "192.168.0.1"))
> $a.pop()
IPAddressToString : 192.168.0.1
Address           : 16820416
AddressFamily     : InterNetwork
ScopeId           :
IsIPv6Multicast   : False
IsIPv6LinkLocal   : False
IsIPv6SiteLocal   : False

Have Fun

Tags:       


Comments:
How do you invoke a generic method from Powershell?
 

Post a Comment





<< Home