Use non-virtual call to invoke 'family or assembly' methods on base class from PowerShell class (#7622) (#7624)

This commit is contained in:
Yuriy Kushnir
2018-08-28 22:24:06 -07:00
committed by Dongbo Wang
parent 358e8abefe
commit 165f2c9a42
2 changed files with 64 additions and 2 deletions
@@ -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}