some work work on api

This commit is contained in:
2024-08-15 09:19:21 +02:00
parent 02a6c3bf6a
commit 4df972c7a4
8 changed files with 194 additions and 39 deletions

View 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);
}
}