mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Use non-virtual call to invoke 'family or assembly' methods on base class from PowerShell class (#7622) (#7624)
This commit is contained in:
committed by
Travis Plunk
parent
28c59a2a57
commit
d8bb2e9028
@@ -219,7 +219,69 @@ Describe 'Classes methods with inheritance' -Tags "CI" {
|
||||
[baz]::new().foo() | Should -Be 200600
|
||||
}
|
||||
|
||||
It 'allows base class method call and doesn''t fall into recursion' {
|
||||
It 'allows base .NET class method call and doesn''t fall into recursion' {
|
||||
Add-Type -TypeDefinition @'
|
||||
public class BaseMembersTestClass
|
||||
{
|
||||
public virtual int PublicMethod()
|
||||
{
|
||||
return 1001;
|
||||
}
|
||||
|
||||
protected virtual int FamilyMethod()
|
||||
{
|
||||
return 2002;
|
||||
}
|
||||
|
||||
protected internal virtual int FamilyOrAssemblyMethod()
|
||||
{
|
||||
return 3003;
|
||||
}
|
||||
}
|
||||
'@
|
||||
$derived = Invoke-Expression @'
|
||||
class BaseCallTestClass : BaseMembersTestClass
|
||||
{
|
||||
hidden [int] $publicMethodCallCounter
|
||||
[int] PublicMethod()
|
||||
{
|
||||
if ($this.publicMethodCallCounter++ -gt 0)
|
||||
{
|
||||
throw "Recursion happens"
|
||||
}
|
||||
return 3 * ([BaseMembersTestClass]$this).PublicMethod()
|
||||
}
|
||||
|
||||
hidden [int] $familyMethodCallCounter
|
||||
[int] FamilyMethod()
|
||||
{
|
||||
if ($this.familyMethodCallCounter++ -gt 0)
|
||||
{
|
||||
throw "Recursion happens"
|
||||
}
|
||||
return 3 * ([BaseMembersTestClass]$this).FamilyMethod()
|
||||
}
|
||||
|
||||
hidden [int] $familyOrAssemblyMethodCallCounter
|
||||
[int] FamilyOrAssemblyMethod()
|
||||
{
|
||||
if ($this.familyOrAssemblyMethodCallCounter++ -gt 0)
|
||||
{
|
||||
throw "Recursion happens"
|
||||
}
|
||||
return 3 * ([BaseMembersTestClass]$this).FamilyOrAssemblyMethod()
|
||||
}
|
||||
}
|
||||
|
||||
[BaseCallTestClass]::new()
|
||||
'@
|
||||
|
||||
$derived.PublicMethod() | Should -Be 3003
|
||||
$derived.FamilyMethod() | Should -Be 6006
|
||||
$derived.FamilyOrAssemblyMethod() | Should -Be 9009
|
||||
}
|
||||
|
||||
It 'allows base PowerShell class method call and doesn''t fall into recursion' {
|
||||
class bar
|
||||
{
|
||||
[int]foo() {return 1001}
|
||||
|
||||
Reference in New Issue
Block a user