You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling base class method in a System.Xml.XmlNode-derived custom class fails; invokes own method instead, resulting in infinite recursion #6386
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
classchild : 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.
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)
This is not specific to powershell class. It repros with a regular type added with Add-Type:
$code =@'
public class Foo : System.Xml.XmlDocument {
public override void LoadXml(string content) {
System.Console.WriteLine("BLAH");
}
}
'@
Add-Type $code
$foo = [Foo]::new()
([System.Xml.XmlDocument]$foo).LoadXml('<foo>bar</foo>')
> BLAH
This is actually specpfic to types that derive from System.Xml.XmlNode. The primary adapter of such a type is the XmlNodeAdapter, which is not optimizable (SiteBinderCanOptimize returns false). In such case, the invocation is done through the Invoke method of the adapter, instead of PSInvokeMemberBinder.InvokeDotNetMethod, and therefore, the PSMethodInvocationConstraints is not respected.
changed the title [-]Calling base class method in a custom class fails; invokes own method instead, resulting in infinite recursion[/-][+]Calling base class method in a System.Xml.XmlNode-derived custom class fails; invokes own method instead, resulting in infinite recursion[/+]on Mar 14, 2018
Activity
BrucePay commentedon Mar 13, 2018
It seems to work, at least in simple cases, when you are extending a PowerShell class as in:
Jason Shirk (@lzybkr) Thoughts?
lzybkr commentedon Mar 13, 2018
Sorry, no idea what is going on here.
daxian-dbw commentedon Mar 14, 2018
This is not specific to powershell class. It repros with a regular type added with
Add-Type:This is actually specpfic to types that derive from
System.Xml.XmlNode. The primary adapter of such a type is theXmlNodeAdapter, which is not optimizable (SiteBinderCanOptimizereturnsfalse). In such case, the invocation is done through theInvokemethod of the adapter, instead ofPSInvokeMemberBinder.InvokeDotNetMethod, and therefore, thePSMethodInvocationConstraintsis not respected.[-]Calling base class method in a custom class fails; invokes own method instead, resulting in infinite recursion[/-][+]Calling base class method in a System.Xml.XmlNode-derived custom class fails; invokes own method instead, resulting in infinite recursion[/+]