24/7/365 Support

Program: Create Instances of Generic Objects .NET Framework

When you work with the .NET Framework, you’ll often run across classes that have the primary responsibility of managing other objects. For example, the System. Collections.ArrayList class lets you manage a dynamic list of objects. You can add objects to an ArrayList, remove objects from it, sort the objects inside, and more. These objects can be any type of object—String objects, integers, DateTime objects, and many more. However, working with classes that support arbitrary objects can sometimes be a little awkward. One example is type safety: if you accidentally add a String to a list of integers, you might not find out until your program fails.

Although the issue becomes largely moot when working only inside PowerShell, a more common complaint in strongly typed languages (such as C#) is that you have to remind the environment (through explicit casts) about the type of your object when you work with it again:

// This is C# code

System.Collections.ArrayList list =

new System.Collections.ArrayList();

list.Add("Hello World");

string result = (String) list[0];

To address these problems, the .NET Framework introduced a feature called generic types: classes that support arbitrary types of objects, but allow you to specify which type of object. In this case, a collection of strings:

// This is C# code System.Collections.ObjectModel.Collection<String> list = new System.Collections.ObjectModel.Collection<String>(); list.Add("Hello World");

string result = list[0]; Although the NewObject cmdlet is powerful, it doesn’t yet handle creating generic types very elegantly. For a simple generic type, you can use the syntax that the .NET Framework uses under the hood:

$coll = NewObject 'System.Collections.ObjectModel.Collection`1[System.String]'

However, that begins to fall apart if you want to use types defined outside the main mscorlib assembly, or want to create complex generic types (for example, ones that refer to other generic types).

Example 33. NewGenericObject.ps1

############################################################################## ## ## NewGenericObject.ps1 ## ## Creates an object of a generic type: ## ## Usage:

##

##
# Simple generic collection

##
NewGenericObject System.Collections.ObjectModel.Collection System.Int32

##

##
# Generic dictionary with two types

##
NewGenericObject System.Collections.Generic.Dictionary `

##
System.String,System.Int32

##

##
# Generic list as the second type to a generic dictionary

##
$secondType = NewGenericObject System.Collections.Generic.List Int32

##
NewGenericObject System.Collections.Dictionary `

##
System.String,$secondType.GetType()

##

##
# Generic type with a nondefault constructor

##
NewGenericObject System.Collections.Generic.LinkedListNode `

##
System.String "Hi"

##

##############################################################################

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say