Repository navigation
Add MethodInfo.CreateDelegate<TDelegate> polyfills #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e4c237
Add MethodInfo.CreateDelegate<T> polyfills
Copilot 2e4fd5b
Update MethodInfoTests.cs
Tyrrrz 44f93f6
Unify MethodInfo CreateDelegate fallbacks
Copilot ccf4b57
Correct CreateDelegate API introduction version
Copilot a1ebe73
Plan global MethodInfo extension imports
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.Reflection; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net50; | ||
|
|
||
| public class MethodInfoTests | ||
| { | ||
| private static int Add(int a, int b) => a + b; | ||
|
|
||
| private class Multiplier(int factor) | ||
| { | ||
| public int Multiply(int value) => value * factor; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateDelegate_Test() | ||
| { | ||
| // Arrange | ||
| var method = typeof(MethodInfoTests).GetMethod( | ||
| nameof(Add), | ||
| System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | ||
| )!; | ||
|
|
||
| // Act | ||
| var del = method.CreateDelegate<Func<int, int, int>>(); | ||
|
|
||
| // Assert | ||
| del(2, 3).Should().Be(5); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateDelegate_WithTarget_Test() | ||
| { | ||
| // Arrange | ||
| var instance = new Multiplier(2); | ||
| var method = typeof(Multiplier).GetMethod(nameof(Multiplier.Multiply))!; | ||
|
|
||
| // Act | ||
| var del = method.CreateDelegate<Func<int, int>>(instance); | ||
|
|
||
| // Assert | ||
| del(3).Should().Be(6); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| #pragma warning disable CS0436 | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace System.Reflection; | ||
|
|
||
| #if !POLYSHIM_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal static class MemberPolyfills_Net50_MethodInfo | ||
| { | ||
| extension(MethodInfo method) | ||
| { | ||
| #if NETFRAMEWORK && !NET45_OR_GREATER | ||
| // https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate(system-type) | ||
| public Delegate CreateDelegate(Type delegateType) => | ||
| Delegate.CreateDelegate(delegateType, method); | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate(system-type-system-object) | ||
| public Delegate CreateDelegate(Type delegateType, object? target) => | ||
| Delegate.CreateDelegate(delegateType, target, method); | ||
| #endif | ||
|
Tyrrrz marked this conversation as resolved.
Resolved
|
||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate-1 | ||
| public TDelegate CreateDelegate<TDelegate>() | ||
| where TDelegate : Delegate => | ||
| (TDelegate)(object)method.CreateDelegate(typeof(TDelegate)); | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate-1(system-object) | ||
| public TDelegate CreateDelegate<TDelegate>(object? target) | ||
| where TDelegate : Delegate => | ||
| (TDelegate)(object)method.CreateDelegate(typeof(TDelegate), target); | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.