Skip to content

Commit

Permalink
Add GetMethod(string,int,BindingFlags,Type[]) (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Oct 17, 2024
1 parent b734f29 commit 9b75b1f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 366**
**API count: 367**
1 change: 1 addition & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@

#### Type

* `Reflection.MethodInfo GetMethod(String, Int32, Reflection.BindingFlags, Type[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.type.getmethod#system-type-getmethod(system-string-system-int32-system-reflection-bindingflags-system-type()))
* `Boolean IsAssignableFrom<T>()`
* `Boolean IsAssignableTo<T>()`
* `Boolean IsAssignableTo(Type)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignableto)
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`


**API count: 366**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 367**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -829,6 +829,7 @@ The class `Polyfill` includes the following extension methods:

#### Type

* `Reflection.MethodInfo GetMethod(String, Int32, Reflection.BindingFlags, Type[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.type.getmethod#system-type-getmethod(system-string-system-int32-system-reflection-bindingflags-system-type()))
* `Boolean IsAssignableFrom<T>()`
* `Boolean IsAssignableTo<T>()`
* `Boolean IsAssignableTo(Type)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignableto)
Expand Down
15 changes: 15 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ async Task CancellationTokenSource_Methods()
await source.CancelAsync();
}


#if !NETFRAMEWORK && !NETSTANDARD2_0 && !NETCOREAPP2_0
class WithGenericMethod
{
public void GenericMethod<T>(string value)
{
}
}

void Type_GetMethod()
{
var type = typeof(WithGenericMethod);
type.GetMethod("GenericMethod", 1, BindingFlags.Public, [typeof(string)]);
}
#endif
void ConcurrentDictionary_Methods()
{
var dict = new ConcurrentDictionary<string, int>();
Expand Down
10 changes: 10 additions & 0 deletions src/Polyfill/Polyfill_Type.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

using System.Linq;

#pragma warning disable

namespace Polyfills;
Expand All @@ -17,6 +20,13 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
target.Module.Equals(other.Module);
#endif

#if !NET9_0_OR_GREATER && !NETFRAMEWORK && !NETSTANDARD2_0 && !NETCOREAPP2_0
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.type.getmethod#system-type-getmethod(system-string-system-int32-system-reflection-bindingflags-system-type())")]
public static MethodInfo? GetMethod(this Type target, string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types) =>
target.GetMethod(name, genericParameterCount, bindingAttr, null, types, null);
#endif

/// <summary>
/// Gets a value that indicates whether the current Type represents a type parameter in the definition of a generic method.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Tests/PolyfillTests_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,32 @@ public void IsAssignableFrom()
Assert.True(typeof(IList).IsAssignableFrom<List<string>>());
Assert.False(typeof(string).IsAssignableFrom<List<string>>());
}

public class WithGenericMethod
{
public void Method<T>(string value)
{
}
public void Method<T>()
{
}
public void Method<T, K>(string value)
{
}
public void Method<T>(string value1, string value2)
{
}
}

#if !NETFRAMEWORK && !NETSTANDARD2_0 && !NETCOREAPP2_0
[Test]
public void Type_GetMethod()
{
var type = typeof(WithGenericMethod);
var method = type.GetMethod("Method", 1, BindingFlags.Public | BindingFlags.Instance, [typeof(string)])!;
Assert.AreEqual("Method", method.Name);
Assert.AreEqual(1, method.GetParameters().Length);
Assert.AreEqual(1, method.GetGenericArguments().Length);
}
#endif
}

0 comments on commit 9b75b1f

Please sign in to comment.