Skip to content

Commit

Permalink
Improve .NET overload definition of generics
Browse files Browse the repository at this point in the history
This change ensures that generic methods created with
MakeGenericMethod() will include the generic type information in the
overload definition string so that consumers can easily see it is from
a generic overload rather than another overload.
  • Loading branch information
jborean93 committed May 23, 2024
1 parent 74d8bdb commit ad04d27
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/CoreAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4441,7 +4441,7 @@ internal static string GetMethodInfoOverloadDefinition(string memberName, Method
}

builder.Append(memberName ?? methodEntry.Name);
if (methodEntry.IsGenericMethodDefinition)
if (methodEntry.IsGenericMethodDefinition || methodEntry.IsGenericMethod)
{
builder.Append('[');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,50 @@ namespace TestParameterizedPropertyDefinition {
$result.Definition | Should -BeExactly "int TheItem(int i) {get;}"
}
}

Context "Verify Definition of CodeMethod" {
BeforeAll {
Add-Type -TypeDefinition @"
using System.Management.Automation;
namespace TestCodeMethodDefinition;
public static class TestClass1
{
public static T Generic1<T>(PSObject obj, T val) => val;
public static T1 Generic2<T1, T2>(PSObject obj, T1 val, T2 dummy) => val;
}
"@

$mi1 = [TestCodeMethodDefinition.TestClass1].GetMethod("Generic1")
$mi2 = [TestCodeMethodDefinition.TestClass1].GetMethod("Generic2")

$obj = [PSCustomObject]@{}
$obj | Add-Member -MemberType CodeMethod -Name Generic1 -Value $mi1
$obj | Add-Member -MemberType CodeMethod -Name Generic2 -Value $mi2
$obj | Add-Member -MemberType CodeMethod -Name FromGeneric1 -Value $mi1.MakeGenericMethod(@([int]))
$obj | Add-Member -MemberType CodeMethod -Name FromGeneric2 -Value $mi2.MakeGenericMethod(@([string], [int]))
}

It "Get definition of CodeMethod with generic method of 1 type" {
$result = $obj | Get-Member -Name Generic1
$result.Definition | Should -BeExactly "static T Generic1[T](psobject obj, T val)"
}

It "Get definition of CodeMethod with generic method of 2 types" {
$result = $obj | Get-Member -Name Generic2
$result.Definition | Should -BeExactly "static T1 Generic2[T1, T2](psobject obj, T1 val, T2 dummy)"
}

It "Get definition of CodeMethod with constructed generic method of 1 type" {
$result = $obj | Get-Member -Name FromGeneric1
$result.Definition | Should -BeExactly "static int Generic1[int](psobject obj, int val)"
}

It "Get definition of CodeMethod with constructed generic method of 2 types" {
$result = $obj | Get-Member -Name FromGeneric2
$result.Definition | Should -BeExactly "static string Generic2[string, int](psobject obj, string val, int dummy)"
}
}
}

0 comments on commit ad04d27

Please sign in to comment.