Creating a Cloneable Class in WMF 5.0

Introduction

You might have noticed that instances of certain types of classes are created, a method called Clone is available that will create a (shallow) copy of the object. A PowerShell Hashtable object is a classic example of a class that supports the Clone method:

This is nothing new to developers, but for most Ops people it might be something they’re not that familiar with. But if you are an Ops person who is implementing more advanced PowerShell modules or scripts in WMF 5.0 that require custom classes, then this might be something you need to do.

Note: you can do this in WMF 3.0 and 4.0 but it requires reflection and a lot more code, so isn’t something I’m going to cover here.

For this post, I’m assuming you have a basic knowledge of how to create classes in WMF 5.0. If you aren’t familiar with creating classes, take a look at this series for a very good primer.

If you just go and create a new class in PowerShell and try to call the clone method, an error with be thrown:

ss_cloneable_nolonemethoderror

This is because by default a new class that is defined in PowerShell is based off the System.Object class which does not implement the ICloneable interface. The ICloneable interface is what gives us the Clone method on an object. So we need to tell PowerShell that we want our new class to implement the ICloneable interface.

Note: You don’t really need to know what an interface is to use it, but if you do want a better understanding of it, this is a good place to start. Details on the ICloneable Interface can be found here.

Implementing an Interface

Creating a class that implements the ICloneable interface just requires that we add the name of the interface to implement after a colon following the class name:

However, if we try to define this class as is we’ll get an error:

ss_cloneable_classclonemethodnotimplementederror

The problem is that we’ve told PowerShell that the Car class implements ICloneable and should therefore have a Clone method, but we haven’t actually created the Clone method in the class.

To do this we need to add the method to our class definition:

The above code first creates a new Car object, then gets a list of the properties on the existing ($This) object and uses a foreach loop to copy the content of each property to the new Car object ($NewCar). The $NewCar object is returned to the calling code.

Note: This performs a shallow copy of the object. If you want to perform a deep copy then you’ll need to implement code based on the child objects within the existing object.

You’ve now created an class that implements an interface. The .NET framework provides hundreds (if not thousands) of interfaces that you potentially could implement on your PowerShell classes. Of course, you could have cloned the Car object without implementing the ICloneable interface at all, but this post is intended to be a general introduction to implementing interfaces in WMF 5.0 as well as the ICloneable interface.

Thanks for reading.

2 thoughts on “Creating a Cloneable Class in WMF 5.0

  1. Classes is definitely something worth looking into if only to get that Dev feeling…🙂

    So I tried the code, did a little refactoring… I know it wasn’t part of the clone… Hope you don’t mind…

    So what are you working on that you need clone()? Classes will definitely give you an edge! Looking forward to the next post!

    Rg./Irwin

    Like

    • Nice! Thanks Irwin. Always cleaner to have a couple of constructors in a class. I’ve been throwing a lot of time into a project called LabBuilder (https://github.com/PlagueHO/LabBuilder). It creates Hyper-V lab environments from an XML file using DSC Library configurations etc. The Lab elements (Templates, VMs, Switches, VHDs etc) were being internally represented using Hashtables (quick and dirty approach), but now as more people are using it I decided to clean it up – ran smack into the problem because my unit tests often clone the objects to make the code trimmer. I come from the Dev side of things so it was an easy fix, but lots of Ops people aren’t so familiar with it. Definitely getting into the dev side of things🙂 Feel free to throw any code at the LabBuilder project if you feel a use for it!

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s