Powershell从重写函数调用基类函数

发布于 2025-01-13 21:53:33 字数 495 浏览 3 评论 0原文

我想从其覆盖的函数中调用父函数,我在以下代码中隔离了我的问题:

class SomeClass{
  [type]GetType(){
    write-host 'hooked'
    return $BaseClass.GetType() # how do i call the BaseClass GetType function??
  }
}
SomeClass::new().GetType()

我期待这样的输出:

hooked
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     SomeClass                                System.Object

I want to make a call to the parent function from its overidden function, i isolated my problem in the following code:

class SomeClass{
  [type]GetType(){
    write-host 'hooked'
    return $BaseClass.GetType() # how do i call the BaseClass GetType function??
  }
}
SomeClass::new().GetType()

i am expecting an output like this:

hooked
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     SomeClass                                System.Object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2025-01-20 21:53:33

为了调用上的方法,强制转换 $this 到它 > (([object] $this).GetType(),在您的情况下,考虑到您的 class隐式派生自[object](System.Object)):

class SomeClass  {
  [type]GetType(){
    Write-Verbose -Verbose hooked!
    # Casting to [object] calls the original .GetType() method.
    return ([object] $this).GetType()
  }
}

[SomeClass]::new().GetType()

作为旁白,参考 PowerShell 中的基类 自定义类

  • < p>PowerShell 仅允许您通过基类构造函数调用中的抽象 base 标识符来引用基类构造函数调用

    class Foo { [int] $Num; Foo([int] $Num) { $this.Num = $Num } }
    class FooSub : Foo { FooSub() : base(42) { } } # 注意`:base(...)`部分
    [FooSub]::new().Num # -> 42
    
  • 在 < em>方法引用基类的唯一(基于非反射的)方法是通过类型文字 (强制转换),本质上要求您硬编码基类名称(也如上面的 ([object] $this) 所示):

    class Foo { [string] Method() { return 'hi' } }
    # 请注意需要显式命名基类,如 [Foo]。
    class FooSub : Foo { [string] Method() { return ([Foo] $this).Method() + '!' } }
    [FooSub]::new().Method() # -> '你好!'
    
    • 警告Windows PowerShell 中似乎存在一个错误PowerShell (Core) 7+),其中调用基类'如果基类是,方法将失败使用类型参数 [pscustomobject] 又名 [psobject] 构造的通用类型。在这种情况下,请使用基于反射的解决方法;例如:

      class Foo : System.Collections.ObjectModel.Collection[pscustomobject] {
        [void] 添加([pscustomobject] $item) {
          Write-Verbose -Verbose 上瘾了!
          # 使用反射调用基类的 .Add() 方法。
          # 注意:在 *PowerShell Core* 中,此解决方法不是必需的,通常的方法即可:
          # ([System.Collections.ObjectModel.Collection[pscustomobject]] $this).Add($item)
          # 注意.GetMethod()和.Invoke()的使用
          [System.Collections.ObjectModel.Collection[pscustomobject]].GetMethod('Add').Invoke($this, [object[]] $item)
        }
      }
      

In oder to call a method on the base class, cast $this to it (([object] $this).GetType(), in your case, given that your class implicitly derives from [object](System.Object)):

class SomeClass  {
  [type]GetType(){
    Write-Verbose -Verbose hooked!
    # Casting to [object] calls the original .GetType() method.
    return ([object] $this).GetType()
  }
}

[SomeClass]::new().GetType()

As an aside re referring to the base class in PowerShell custom classes:

  • PowerShell only allows you to reference the base class via the abstract base identifier in base-class constructor calls:

    class Foo { [int] $Num; Foo([int] $Num) { $this.Num = $Num } }
    class FooSub : Foo { FooSub() : base(42) { } } # Note the `: base(...)` part
    [FooSub]::new().Num # -> 42
    
  • In methods, the only (non-reflection-based) way to refer to the base class is via a type literal (cast), which in essence requires you to hard-code the base-class name (as also shown with ([object] $this) above):

    class Foo { [string] Method() { return 'hi' } }
    # Note the need to name the base class explicitly, as [Foo].
    class FooSub : Foo { [string] Method() { return ([Foo] $this).Method() + '!' } }
    [FooSub]::new().Method() # -> 'hi!'
    
    • Caveat: There appears to be a bug in Windows PowerShell (no longer present in PowerShell (Core) 7+), where calling a base class' method fails if the base class is a generic type that is constructed with type argument [pscustomobject] aka [psobject]. Use a reflection-based workaround in that case; e.g.:

      class Foo : System.Collections.ObjectModel.Collection[pscustomobject] {
        [void] Add([pscustomobject] $item) {
          Write-Verbose -Verbose hooked!
          # Use reflection to call the base class' .Add() method.
          # Note: In *PowerShell Core*, this workaround isn't necessary, and the usual would work:
          #         ([System.Collections.ObjectModel.Collection[pscustomobject]] $this).Add($item)
          # Note the use of .GetMethod() and .Invoke()
          [System.Collections.ObjectModel.Collection[pscustomobject]].GetMethod('Add').Invoke($this, [object[]] $item)
        }
      }
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文