I have an enumeration, Fruit
, and a class, FruitCollection
, which derives Collection<Fruit>
. I couldn't find a way to clone FruitCollection
using .NET and I found this MSDN article which defined a DeepClone()
function and used MemberwiseClone()
. Now, since this is an enumeration, I don't think I need to "deep" clone it, so I thought MemberwiseClone()
would be sufficient. However, when I try it in PowerShell, the cloned object seems to simply be a pointer to the original object and not a clone. What am I doing wrong?
Is there another way to simply clone a Collection
? FruitCollection
has no other custom members.
C# code:
public enum Fruit
{
Apple = 1,
Orange = 2
}
public class FruitCollection : Collection<Fruit>
{
public FruitCollection Clone()
{
return Clone(this);
}
public static FruitCollection Clone(FruitCollection fruitCollection)
{
return (FruitCollection)fruitCollection.MemberwiseClone();
}
}
PowerShell Output:
PS> $basket1 = New-Object TestLibrary.FruitCollection
PS> $basket1.Add([TestLibrary.Fruit]::Apple)
PS> $basket2 = $basket1.Clone()
PS> $basket1.Add([TestLibrary.Fruit]::Orange)
PS> $basket2
Apple
Orange