I'm attempting to utilize PowerShell's inheritance to create a class that inherits from an existing class and I am not seeing the expected behavior based on various sources.
Here's a brief example highlighting the problem:
class child : System.Xml.XmlDocument {
[String] ToString(){
return 'ChildString'
}
[void] LoadXml([string]$content) {
Write-Host 'LoadXml'
([System.Xml.XmlDocument]$this).LoadXml($content)
}
}
If I run
Write-Host 'XMLDocument Object'
$xml=New-Object xml
$xml.ToString()
Write-Host 'Inherited Object'
$child=New-Object child
$child.ToString()
The console output is
XMLDocument Object
#document
Inherited Object
#document
indicating that the ToString()
method of the child class is NOT being utilized.
On the opposite end of the spectrum, if I call $child.LoadXml('<a>Hello</a>')
then I get infinite recursion, implying that the call to the parent object is failing in that method.
Based on what I've been able to find I would not expect these methods to behave this way and I was hoping that someone could help me understand what I am missing. I am generally more used to Python; however, for security reasons I can't implement this code in another programming language (I can't install software on the computer this is to be run on).
Thanks!
Here's the results of $PSversionTable.PSVersion
for reference:
Major Minor Build Revision
----- ----- ----- --------
5 1 15063 909