2024-10-10 10:45:53 +08:00
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2024-10-27 00:54:10 +08:00
|
|
|
|
using System.Reflection;
|
2025-03-14 16:04:06 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2024-10-10 10:45:53 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using static Serein.Library.Utils.EmitHelper;
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
namespace Serein.Library
|
2024-10-10 10:45:53 +08:00
|
|
|
|
{
|
2024-10-10 16:49:37 +08:00
|
|
|
|
/// <summary>
|
2024-10-11 19:31:34 +08:00
|
|
|
|
/// Emit创建的委托描述,用于WebApi、WebSocket、NodeFlow动态调用方法的场景。
|
|
|
|
|
|
/// 一般情况下你无须内部细节,只需要调用 Invoke() 方法即可。
|
2024-10-10 16:49:37 +08:00
|
|
|
|
/// </summary>
|
2024-10-10 10:45:53 +08:00
|
|
|
|
public class DelegateDetails
|
|
|
|
|
|
{
|
2024-10-27 00:54:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据方法信息构建Emit委托
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="methodInfo"></param>
|
|
|
|
|
|
public DelegateDetails(MethodInfo methodInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
var emitMethodType = EmitHelper.CreateDynamicMethod(methodInfo, out var emitDelegate);
|
2024-12-20 23:39:29 +08:00
|
|
|
|
_emitMethodInfo = emitMethodType;
|
2024-10-27 00:54:10 +08:00
|
|
|
|
_emitDelegate = emitDelegate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-04 23:30:52 +08:00
|
|
|
|
|
2025-03-14 16:04:06 +08:00
|
|
|
|
|
2024-11-04 23:30:52 +08:00
|
|
|
|
/*/// <summary>
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// 更新委托方法
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="EmitMethodType"></param>
|
|
|
|
|
|
/// <param name="EmitDelegate"></param>
|
2024-10-10 10:45:53 +08:00
|
|
|
|
public void Upload(EmitMethodType EmitMethodType, Delegate EmitDelegate)
|
|
|
|
|
|
{
|
|
|
|
|
|
_emitMethodType = EmitMethodType;
|
|
|
|
|
|
_emitDelegate = EmitDelegate;
|
2024-11-04 23:30:52 +08:00
|
|
|
|
}*/
|
|
|
|
|
|
|
2024-10-10 10:45:53 +08:00
|
|
|
|
private Delegate _emitDelegate;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
private EmitMethodInfo _emitMethodInfo;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 该Emit委托的相应信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public EmitMethodInfo EmitMethodInfo => _emitMethodInfo;
|
2024-10-15 10:55:41 +08:00
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
///// <summary>
|
|
|
|
|
|
///// <para>普通方法:Func<object,object[],object></para>
|
|
|
|
|
|
///// <para>异步方法:Func<object,object[],Task></para>
|
|
|
|
|
|
///// <para>异步有返回值方法:Func<object,object[],Task<object>></para>
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
//public Delegate EmitDelegate { get => _emitDelegate; }
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
|
///// 表示Emit构造的委托类型
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
//public EmitMethodType EmitMethodType { get => _emitMethodType; }
|
2024-10-10 20:52:19 +08:00
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<object> AutoInvokeAsync(object[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_emitMethodInfo.IsStatic)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await InvokeAsync(null, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var obj = Activator.CreateInstance(_emitMethodInfo.DeclaringType);
|
|
|
|
|
|
return await InvokeAsync(obj, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new Exception("Not static method");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-11 19:31:34 +08:00
|
|
|
|
/// <summary>
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <para>使用的实例必须能够正确调用该委托,传入的参数也必须符合方法入参信息。</para>
|
|
|
|
|
|
/// </summary>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="instance">拥有符合委托签名的方法信息的实例</param>
|
|
|
|
|
|
/// <param name="args">如果方法没有入参,也需要传入一个空数组</param>
|
2024-10-15 10:55:41 +08:00
|
|
|
|
/// <returns>void方法自动返回null</returns>
|
|
|
|
|
|
public async Task<object> InvokeAsync(object instance, object[] args)
|
2024-10-10 20:52:19 +08:00
|
|
|
|
{
|
2024-10-27 00:54:10 +08:00
|
|
|
|
if (args is null)
|
2024-10-15 10:55:41 +08:00
|
|
|
|
{
|
2024-10-20 12:10:57 +08:00
|
|
|
|
args = Array.Empty<object>();
|
2024-10-15 10:55:41 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
if(_emitMethodInfo.IsStatic)
|
|
|
|
|
|
{
|
|
|
|
|
|
instance = null;
|
|
|
|
|
|
}
|
2024-10-10 20:52:19 +08:00
|
|
|
|
object result = null;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
if (_emitDelegate is Func<object, object[], Task<object>> hasResultTask)
|
2024-10-10 20:52:19 +08:00
|
|
|
|
{
|
2024-12-09 22:57:06 +08:00
|
|
|
|
result = await hasResultTask(instance, args);
|
2024-10-10 20:52:19 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
else if (_emitDelegate is Func<object, object[], Task> task)
|
2024-10-10 20:52:19 +08:00
|
|
|
|
{
|
2024-12-09 22:57:06 +08:00
|
|
|
|
await task.Invoke(instance, args);
|
2024-10-10 20:52:19 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
else if (_emitDelegate is Func<object, object[], object> func)
|
2024-12-09 22:57:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
result = func.Invoke(instance, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException("创建了非预期委托(应该不会出现)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
//try
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
//catch
|
|
|
|
|
|
//{
|
|
|
|
|
|
// throw;
|
|
|
|
|
|
//}
|
2024-10-10 20:52:19 +08:00
|
|
|
|
}
|
2024-10-10 10:45:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|