some work work on api
This commit is contained in:
13
Pilz.Extensions/Pilz.Extensions.csproj
Normal file
13
Pilz.Extensions/Pilz.Extensions.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>2.0.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
24
Pilz.Extensions/Reflection/MethodInfoExtensions.cs
Normal file
24
Pilz.Extensions/Reflection/MethodInfoExtensions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Pilz.Extensions.Reflection;
|
||||
|
||||
public static class MethodInfoExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Source: https://stackoverflow.com/questions/940675/getting-a-delegate-from-methodinfo
|
||||
/// </summary>
|
||||
/// <param name="methodInfo"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static Delegate CreateDelegate(this MethodInfo methodInfo, object? target)
|
||||
{
|
||||
var parmTypes = methodInfo.GetParameters().Select(parm => parm.ParameterType);
|
||||
var parmAndReturnTypes = parmTypes.Append(methodInfo.ReturnType).ToArray();
|
||||
var delegateType = Expression.GetDelegateType(parmAndReturnTypes);
|
||||
|
||||
if (methodInfo.IsStatic)
|
||||
return methodInfo.CreateDelegate(delegateType);
|
||||
return methodInfo.CreateDelegate(delegateType, target);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user