Skip to content

Commit 7e7a4b2

Browse files
authored
Add MethodInfo.CrateDelegate<T> (#306)
1 parent 82195af commit 7e7a4b2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
#if !NET5_0_OR_GREATER
5+
6+
namespace Polyfills;
7+
8+
using System;
9+
using System.Reflection;
10+
11+
static partial class Polyfill
12+
{
13+
/// <summary>
14+
/// Creates a delegate of type T with the specified target from this method.
15+
/// </summary>
16+
/// <typeparam name="T">The type of the delegate to create.</typeparam>
17+
/// <returns>The delegate for this method.</returns>
18+
//Link: https://learn.microsoft.com/en-us/dotnet/api/System.Reflection.MethodInfo.CreateDelegate#system-reflection-methodinfo-createdelegate-1
19+
public static T CreateDelegate<T>(this MethodInfo method) where T : Delegate => (T)method.CreateDelegate(typeof(T));
20+
21+
/// <summary>
22+
/// Creates a delegate of type T with the specified target from this method.
23+
/// </summary>
24+
/// <param name="target">The object targeted by the delegate.</param>
25+
/// <typeparam name="T">The type of the delegate to create.</typeparam>
26+
/// <returns>The delegate for this method.</returns>
27+
//Link: https://learn.microsoft.com/en-us/dotnet/api/System.Reflection.MethodInfo.CreateDelegate?#system-reflection-methodinfo-createdelegate-1(system-object)
28+
public static T CreateDelegate<T>(this MethodInfo method, object? target) where T : Delegate => (T)method.CreateDelegate(typeof(T), target);
29+
}
30+
#endif

0 commit comments

Comments
 (0)