Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions PolyShim.Tests/Net50/MethodInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
Comment thread
Copilot marked this conversation as resolved.
Resolved
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);
}
}
38 changes: 38 additions & 0 deletions PolyShim/Net50/MethodInfo.cs
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
Comment thread
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
9 changes: 7 additions & 2 deletions Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 593
- **Total:** 597
- **Types:** 119
- **Members:** 474
- **Members:** 478

___

Expand Down Expand Up @@ -393,6 +393,11 @@ ___
- [**[struct]**](https://learn.microsoft.com/dotnet/api/system.memory-1) <sup><sub>.NET Core 2.1</sub></sup>
- `MemoryPool<T>`
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.buffers.memorypool-1) <sup><sub>.NET Core 2.1</sub></sup>
- `MethodInfo`
- [`Delegate CreateDelegate(Type)`](https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate(system-type)) <sup><sub>.NET 5.0</sub></sup>
- [`Delegate CreateDelegate(Type, object?)`](https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate(system-type-system-object)) <sup><sub>.NET 5.0</sub></sup>
- [`TDelegate CreateDelegate<TDelegate>() where TDelegate : Delegate`](https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate-1) <sup><sub>.NET 5.0</sub></sup>
- [`TDelegate CreateDelegate<TDelegate>(object?) where TDelegate : Delegate`](https://learn.microsoft.com/dotnet/api/system.reflection.methodinfo.createdelegate#system-reflection-methodinfo-createdelegate-1(system-object)) <sup><sub>.NET 5.0</sub></sup>
- `Module`
- [`T? GetCustomAttribute<T>() where T : Attribute`](https://learn.microsoft.com/dotnet/api/system.reflection.customattributeextensions.getcustomattribute#system-reflection-customattributeextensions-getcustomattribute-1(system-reflection-module)) <sup><sub>.NET Core 1.0</sub></sup>
- `ModuleInitializerAttribute`
Expand Down