Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Monday, December 29, 2008 10:01 AM

PowerShell Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "G".  For "G" I've picked a feature that was introduced in version 2.0 of the .NET Framework that allows for creating more generic types of objects.  So, today's word is "Generics".

Generics allow you to create generic objects such as collections without being constrained to the specific type of object that it contains.  Generics introduce the "type parameter" that you pass in to the creation of an object that determine the final types of some part of that said object.

generic For you non-programmers out there, you may ask yourself "So what's so great about this".  Well, think of it this way.  In the past if you wanted to create a collection class of some sort, you had to write a new version of the class for each type of object (integer, string, etc), or you had to write a single version of the class that could hold any type of object which means that a lot of custom error handling would have to be included to cover all the available types.  With generics, you can write a collection that can be constrained to only containing a specific subset of types (ie. integers and strings).

So, why would the average PowerShell developer care about accessing generic types?  Well, the most common reason is that some of the .NET classes you'll want to work with make use of generics (including PowerShell itself).

Windows PowerShell in Action by Bruce Payette includes the following examples on working with generics.

Building a generic list

The following helper function illustrates how to create a generic list.  First the full name of the generic object, in this case System.Collections.Generic.List is specified, followed by the number of type parameters (in this case one).  $base is an open generic type becuase it hasn't been bound to any type parameters.  The closed type $qt is then created by binding in the type parameters.  Once the closed type is defined, an instance of it can be created with the New-Object Cmdlet.

function New-GenericList([type] $type)
{
  $base = [System.Collections.Generic.List``1]
  $qt = $base.MakeGenericType(@($type))
  New-Object $qt
}
PS> $intList = New-GenericList int
PS> $intList.Add(1)
PS> $intList
1

Building a generic dictionary

A generic dictionary, similar to a hashtable, is made up of key/value pairs but with more control on the types of the keys and values.  In this example we'll constrain the keys to strings and the values to integers.  In this example the collection type is specified following by the number of type parameters of 2.  The two type parameters are then passed into the MakeGenericType function and finally an instance of the generic dictionary is created with the New-Object Cmdlet.

function New-GenericDictionary([type] $keyType, [type]$valueType)
{
  $base = [System.Collections.Generic.Dictionary``2]
  $ct = $base.MakeGenericType(($keyType, $valueType))
  New-Object $ct
}
PS> $gd = New-GenericDictionary string int
PS> $gd["red"] = 1
PS> $gd["blue" = 2
PS> $gd
Key         Value
--- -----
red 1
blue 2

Generics are not something that you'll likely ever need in your day to day PowerShell scripting tasks.  But, if you find yourself in that situation where you are working with a class that requires them, you'll have the tools you need to get your job done.



Feedback

3/3/2009 9:45 AM
Gravatar My Guest Appearance On The PowerScripting Podcast Is Live
Joe Pruitt
7/30/2009 4:49 AM
Gravatar thanks for share inspiration..
Power shell ABC is very good
social bookmarking dofollow

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 3 and 3 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or