The documented syntax for invoking a base-class method from a PS custom class is
([<base-class>] $this).<method>(), but that doesn't seem to work, as discovered in this SO question.
Instead, the derived class' method itself is invoked, resulting in infinite recursion.
Update: The problem appears to be specific to [System.Xml.XmlNode]-derived classes; see @daxian-dbw's comment below.
A reflection-based workaround can be found in this answer.
Steps to reproduce
class child : System.Xml.XmlDocument {
[void] LoadXml([string]$content) {
Write-Host 'Inside overridden LoadXml.'
# Try to call the base type's .LoadXml() method.
([System.Xml.XmlDocument] $this).LoadXml($content)
}
}
[child]::new().LoadXml('<foo>bar</foo>')
Expected behavior
Inside overridden LoadXml.
The message should print once, and the XML string should be loaded into the instance via the call to the base-class method.
Actual behavior
Inside overridden LoadXml.
Inside overridden LoadXml.
...
The method keeps calling itself and eventually runs into a call-depth overflow.
Environment data
PowerShell Core v6.0.1 on macOS 10.13.3
PowerShell Core v6.0.1 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.1 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
The documented syntax for invoking a base-class method from a PS custom class is
([<base-class>] $this).<method>(), but that doesn't seem to work, as discovered in this SO question.Instead, the derived class' method itself is invoked, resulting in infinite recursion.
Update: The problem appears to be specific to
[System.Xml.XmlNode]-derived classes; see @daxian-dbw's comment below.A reflection-based workaround can be found in this answer.
Steps to reproduce
Expected behavior
The message should print once, and the XML string should be loaded into the instance via the call to the base-class method.
Actual behavior
The method keeps calling itself and eventually runs into a call-depth overflow.
Environment data