Creating an Instance of a Nested Class in PowerShell

Creating .NET objects in PowerShell is fairly straightforward using the New-Object cmdlet, but eventually you might have a situation where you need to create an instance of a nested class which is less obvious. Fortunately, PowerShell 3.0 includes some improvements that makes this possible.

Creating an Instance of a Nested Class


As an example lets assume we have an application that tracks Turkey populations across the nation via GPS tags. In the App we have a Turkey class and a subclass of WildTurkey which also happens to be a nested class. See the following for the full class definitions:

                                        
 public class Turkey
 {
   public Turkey() { }

   public virtual void GetDetails()
   {
     Console.WriteLine("Age: {0}", this.Age);
     Console.WriteLine("Breed: {0}", this.Breed);
   }

   public string Breed
   {
      get;
      set;
   }

   public int Age
   {
      get;
      set;
   }

    /// 
    /// A Wild Turkey is a nested subclass of Turkey
    /// 
    public class WildTurkey : Turkey
    {
    public WildTurkey() { }

    public override void GetDetails()
    {
    base.GetDetails();

    Console.WriteLine("Region: {0}", this.Region);
    }

    public string Region
    {
       get;
       set;
    }
   }
 }

                                        

Assuming your assembly is loaded lets start off by creating a Turkey object using the New-Item cmdlet. This will give us an instance of the base class:

                                        
 #
 # Create a new Turkey Object
 #
 $turkey = New-Object -TypeName TurkeyTrackerApp.Turkey
 
 #
 # List Properties
 # 
 $turkey | Get-Member -Type Properties

 TypeName: TurkeyTrackerApp.Turkey

 Name        MemberType Definition
 ----        ---------- ----------
 Age         Property   int Age {get;set;}
 Breed       Property   string Breed {get;set;}


                                        

Finally, lets create an instance of the nested subclass WildTurkey. This is accomplished by appending a "+" after the base class name. This means you can do "TurkeyTrackerApp.Turkey+WildTurkey" to tell PowerShell about the nested class type:

                                        
 #
 # Create a new Wild Turkey Object
 #
 $wildTurkey = New-Object -TypeName TurkeyTrackerApp.Turkey+WildTurkey
 
 #
 # List Properties
 # 
 $wildTurkey | Get-Member -Type Properties

 TypeName: TurkeyTrackerApp.Turkey+WildTurkey
 
 Name   MemberType Definition
 ----   ---------- ----------
 Age    Property   int Age {get;set;}
 Breed  Property   string Breed {get;set;}
 Region Property   string Region {get;set;}


                                        

That's about it, we can see that the output lists all the properties of a WildTurkey.

Bonus Tip: You can load an assembly into a PowerShell session with the Add-Type cmdlet.

  # Load the TurkeyTrackerApp assembly
  Add-Type -Path "G:\Assemblies\TurkeyTrackerApp.dll"

                                        


© 2024 Embrs.net