Skip to content

Calling base class method in a System.Xml.XmlNode-derived custom class fails; invokes own method instead, resulting in infinite recursion #6386

Description

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)

Activity

  1. BrucePay commented on Mar 13, 2018

    @BrucePay
    Collaborator

    It seems to work, at least in simple cases, when you are extending a PowerShell class as in:

    PS[1] (110) > class foo { [void] doit() {Write-Host "DOIT" }}
    PS[1] (111) > class bar : foo { [void] doit() {([foo] $this).doit()}}
    PS[1] (112) > $b.doit()
    DOIT
    

    Jason Shirk (@lzybkr) Thoughts?

  2. lzybkr commented on Mar 13, 2018

    @lzybkr
    Contributor

    Sorry, no idea what is going on here.

  3. daxian-dbw commented on Mar 14, 2018

    @daxian-dbw
    Member

    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.

  4. 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
  5. added
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions