Skip to content

Commit d8bb2e9

Browse files
yurko7TravisEz13
authored andcommitted
Use non-virtual call to invoke 'family or assembly' methods on base class from PowerShell class (#7622) (#7624)
1 parent 28c59a2 commit d8bb2e9

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎src/System.Management.Automation/engine/CoreAdapter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ internal static MethodInformation FindBestMethod(
12301230
var parameterTypes = methodInfo.method.GetParameters().Select(parameter => parameter.ParameterType).ToArray();
12311231
var targetTypeMethod = invocationConstraints.MethodTargetType.GetMethod(methodInfo.method.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null);
12321232

1233-
if (targetTypeMethod != null && (targetTypeMethod.IsPublic || targetTypeMethod.IsFamily))
1233+
if (targetTypeMethod != null && (targetTypeMethod.IsPublic || targetTypeMethod.IsFamily || targetTypeMethod.IsFamilyOrAssembly))
12341234
{
12351235
methodInfo = new MethodInformation(targetTypeMethod, 0);
12361236
callNonVirtually = true;

‎test/powershell/Language/Classes/scripting.Classes.inheritance.tests.ps1‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,69 @@ Describe 'Classes methods with inheritance' -Tags "CI" {
219219
[baz]::new().foo() | Should -Be 200600
220220
}
221221

222-
It 'allows base class method call and doesn''t fall into recursion' {
222+
It 'allows base .NET class method call and doesn''t fall into recursion' {
223+
Add-Type -TypeDefinition @'
224+
public class BaseMembersTestClass
225+
{
226+
public virtual int PublicMethod()
227+
{
228+
return 1001;
229+
}
230+
231+
protected virtual int FamilyMethod()
232+
{
233+
return 2002;
234+
}
235+
236+
protected internal virtual int FamilyOrAssemblyMethod()
237+
{
238+
return 3003;
239+
}
240+
}
241+
'@
242+
$derived = Invoke-Expression @'
243+
class BaseCallTestClass : BaseMembersTestClass
244+
{
245+
hidden [int] $publicMethodCallCounter
246+
[int] PublicMethod()
247+
{
248+
if ($this.publicMethodCallCounter++ -gt 0)
249+
{
250+
throw "Recursion happens"
251+
}
252+
return 3 * ([BaseMembersTestClass]$this).PublicMethod()
253+
}
254+
255+
hidden [int] $familyMethodCallCounter
256+
[int] FamilyMethod()
257+
{
258+
if ($this.familyMethodCallCounter++ -gt 0)
259+
{
260+
throw "Recursion happens"
261+
}
262+
return 3 * ([BaseMembersTestClass]$this).FamilyMethod()
263+
}
264+
265+
hidden [int] $familyOrAssemblyMethodCallCounter
266+
[int] FamilyOrAssemblyMethod()
267+
{
268+
if ($this.familyOrAssemblyMethodCallCounter++ -gt 0)
269+
{
270+
throw "Recursion happens"
271+
}
272+
return 3 * ([BaseMembersTestClass]$this).FamilyOrAssemblyMethod()
273+
}
274+
}
275+
276+
[BaseCallTestClass]::new()
277+
'@
278+
279+
$derived.PublicMethod() | Should -Be 3003
280+
$derived.FamilyMethod() | Should -Be 6006
281+
$derived.FamilyOrAssemblyMethod() | Should -Be 9009
282+
}
283+
284+
It 'allows base PowerShell class method call and doesn''t fall into recursion' {
223285
class bar
224286
{
225287
[int]foo() {return 1001}

0 commit comments

Comments
 (0)