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-29 08:24:06 +03:00 committed by Travis Plunk
parent 28c59a2a57
commit d8bb2e9028
2 changed files with 64 additions and 2 deletions

View file

@ -1230,7 +1230,7 @@ namespace System.Management.Automation
var parameterTypes = methodInfo.method.GetParameters().Select(parameter => parameter.ParameterType).ToArray();
var targetTypeMethod = invocationConstraints.MethodTargetType.GetMethod(methodInfo.method.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
if (targetTypeMethod != null && (targetTypeMethod.IsPublic || targetTypeMethod.IsFamily))
if (targetTypeMethod != null && (targetTypeMethod.IsPublic || targetTypeMethod.IsFamily || targetTypeMethod.IsFamilyOrAssembly))
{
methodInfo = new MethodInformation(targetTypeMethod, 0);
callNonVirtually = true;

View file

@ -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}