Overriding methods (Powershell 5.0)

Greetings! I got into this new Powershell 5.0 with some new cool stuff.

Tried to convert one of my c# codes(Sharepoint conversion) to powershell.

I managed to convert everything else, but it seems when I Add a child to HashSet, it doesnt trigger the Overriden Equals method nor the GetHashCode methods.

Anyone got any idea how to either pass these or fix?

My c# version works perfectly, and the problem here is that the program cant find duplicates (which it should when using HashSet).

Some code below:

Class DocTopic : Node { [Int]$Id [Boolean]$IsFile [SharepointObject]$spObject [String]$Name DocTopic([String]$Name, [Boolean]$IsFile, [SharepointObject]$spObject) { $this.Id = 0 $this.IsFile = $IsFile $this.spObject = $spObject $this.Name = $Name } [DocTopic]AddChild([DocTopic]$child) { [Boolean]$childAdded = ([Node]$this).AddChild($child) if ($childAdded) { if (!$child.IsFile -and $child.Name -ne "root") { $child.Id = $script:IncrementId++ } } else { foreach ($node in $this.Children) { [DocTopic]$existingChild = [DocTopic]$node if ($existingChild.Equals($child)) { $child = $existingChild } } } return $child } [Bool]Equals([Object]$obj) { if ($obj -eq $null) { return $false } $other = [DocTopic]$obj return $other -ne $null -and $this.Name -eq $other.Name } [Int]GetHashCode() { return $this.Name.GetHashCode() * 17 } } Class Node { [Int]$Level [Node]$Parent [HashSet[Node]]$Children Node() { $this.Level = 0 $this.Parent = $null $this.Children = [HashSet[Node]]::New() } [Boolean]AddChild([Node]$child) { $unique = $this.Children.Add($child) if (!$unique) { return $false } $child.Level = $this.Level + 1 $child.Parent = $this return $true } [HashSet[Node]]Children() { return $this.Children } } 

submitted by /u/Kuus4
[link] [comments]

Leave a Reply