2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using System;
|
2024-11-04 23:30:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using System.Linq;
|
2024-11-03 21:17:45 +08:00
|
|
|
|
using System.Reflection;
|
2024-10-28 15:21:08 +08:00
|
|
|
|
using System.Text;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 每个节点有独自的MethodDetails实例
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[NodeProperty(ValuePath = NodeValuePath.Method)]
|
2024-10-20 12:10:57 +08:00
|
|
|
|
public partial class MethodDetails
|
|
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
// private readonly IFlowEnvironment env;
|
2024-10-23 19:22:27 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对应的节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo(IsProtection = true)]
|
2025-05-31 12:15:01 +08:00
|
|
|
|
private IFlowNode _nodeModel;
|
2024-10-23 19:22:27 +08:00
|
|
|
|
|
2024-11-03 21:17:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对应的程序集
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private string _assemblyName;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
2024-11-04 23:30:52 +08:00
|
|
|
|
/// 调用节点方法时需要的实例(多个相同的节点将拥有相同的类型)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private Type _actingInstanceType;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 作用实例(多个相同的节点将会共享同一个实例)
|
|
|
|
|
|
/// </summary>
|
2025-03-20 22:54:10 +08:00
|
|
|
|
// [PropertyInfo]
|
|
|
|
|
|
// private object _actingInstance;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 方法名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private string _methodName;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private NodeType _methodDynamicType;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 锁名称(暂未实现)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private string _methodLockName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-28 15:21:08 +08:00
|
|
|
|
/// 方法别名
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
2024-10-28 15:21:08 +08:00
|
|
|
|
private string _methodAnotherName;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 参数描述
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private ParameterDetails[] _parameterDetailss;
|
|
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <para>描述该方法是否存在可选参数</para>
|
|
|
|
|
|
/// <para>-1表示不存在</para>
|
|
|
|
|
|
/// <para>0表示第一个参数是可选参数</para>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
2024-11-02 22:11:38 +08:00
|
|
|
|
private int _paramsArgIndex = -1;
|
2024-11-02 16:48:40 +08:00
|
|
|
|
|
2025-07-07 20:40:24 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否为异步方法(如果为异步方法,则返回值类型为Task或Task<T>)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private bool _isAsync = false;
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 出参类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[PropertyInfo]
|
|
|
|
|
|
private Type _returnType;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public partial class MethodDetails
|
|
|
|
|
|
{
|
2024-11-02 16:48:40 +08:00
|
|
|
|
|
2024-11-02 22:11:38 +08:00
|
|
|
|
#region 更改可变参数
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// <summary>
|
2024-11-02 22:11:38 +08:00
|
|
|
|
/// 是否存在可变参数(-1表示不存在)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool HasParamsArg => _paramsArgIndex >= 0;
|
|
|
|
|
|
|
2025-07-09 21:49:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否为静态方法
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool IsStatic { get; set; }
|
|
|
|
|
|
|
2024-11-02 22:11:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增可变参数
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="index"></param>
|
2025-06-22 21:53:37 +08:00
|
|
|
|
public bool AddParamsArg(int index)
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2025-06-22 21:53:37 +08:00
|
|
|
|
if (ParamsArgIndex < 0 // 方法是否包含可变参数
|
|
|
|
|
|
|| index < 0 // 如果包含,则判断从哪个参数赋值
|
|
|
|
|
|
|| index < ParamsArgIndex // 需要判断是否为可选参数的部分
|
|
|
|
|
|
|| index >= ParameterDetailss.Length) // 防止下标越界
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2025-06-22 21:53:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-09 21:49:26 +08:00
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
var newPd = ParameterDetailss[index].CloneOfModel(this.NodeModel); // 复制出属于本身节点的参数描述
|
|
|
|
|
|
newPd.Index = ParameterDetailss.Length; // 更新索引
|
|
|
|
|
|
newPd.IsParams = true;
|
|
|
|
|
|
ParameterDetailss = ArrayHelper.AddToArray(ParameterDetailss, newPd); // 新增
|
2025-07-09 21:49:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool AddParamsArg(ParameterDetails parameterDetails)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ParamsArgIndex < 0) // 方法是否包含可变参数
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
2024-11-02 22:11:38 +08:00
|
|
|
|
}
|
2025-06-22 21:53:37 +08:00
|
|
|
|
if (parameterDetails is null)
|
2024-11-02 22:11:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
2024-11-02 16:48:40 +08:00
|
|
|
|
}
|
2025-06-22 21:53:37 +08:00
|
|
|
|
parameterDetails.Index = ParameterDetailss.Length; // 更新索引
|
|
|
|
|
|
parameterDetails.IsParams = true;
|
|
|
|
|
|
ParameterDetailss = ArrayHelper.AddToArray(ParameterDetailss, parameterDetails); // 新增
|
2025-07-09 21:49:26 +08:00
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
}
|
2025-06-22 21:53:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// <summary>
|
2024-11-02 22:11:38 +08:00
|
|
|
|
/// 移除可变参数
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="index"></param>
|
2025-06-22 21:53:37 +08:00
|
|
|
|
public bool RemoveParamsArg(int index)
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2025-06-22 21:53:37 +08:00
|
|
|
|
|
|
|
|
|
|
if (ParamsArgIndex < 0 // 方法是否包含可变参数
|
|
|
|
|
|
|| index < 0 // 如果包含,则判断从哪个参数赋值
|
|
|
|
|
|
|| index <= ParamsArgIndex // 需要判断是否为可选参数的部分,并且不能删除原始的可变参数描述
|
|
|
|
|
|
|| index >= ParameterDetailss.Length) // 防止下标越界
|
2024-11-02 16:48:40 +08:00
|
|
|
|
{
|
2025-06-22 21:53:37 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-11-04 23:30:52 +08:00
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
ParameterDetailss[index] = null; // 释放对象引用
|
|
|
|
|
|
var tmp = ArrayHelper.RemoteToArray<ParameterDetails>(ParameterDetailss, index); // 新增;
|
|
|
|
|
|
UpdateParamIndex(ref tmp);
|
|
|
|
|
|
ParameterDetailss = tmp; // 新增
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-11-04 23:30:52 +08:00
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除可变参数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parameterDetails"></param>
|
|
|
|
|
|
public bool RemoveParamsArg(ParameterDetails parameterDetails)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ParamsArgIndex < 0) // 方法是否包含可变参数
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
2024-11-02 22:11:38 +08:00
|
|
|
|
}
|
2025-06-22 21:53:37 +08:00
|
|
|
|
if (parameterDetails is null)
|
2024-11-02 22:11:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-06-22 21:53:37 +08:00
|
|
|
|
int index = -1;
|
|
|
|
|
|
for (int i = 0; i < ParameterDetailss.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var pd = ParameterDetailss[i];
|
|
|
|
|
|
if (pd.Equals(parameterDetails))
|
|
|
|
|
|
{
|
|
|
|
|
|
index = i;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (index == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ParameterDetailss[index] = null; // 释放对象引用
|
|
|
|
|
|
var tmp = ArrayHelper.RemoteToArray<ParameterDetails>(ParameterDetailss, index); // 新增;
|
|
|
|
|
|
UpdateParamIndex(ref tmp);
|
|
|
|
|
|
ParameterDetailss = tmp; // 新增
|
|
|
|
|
|
return true;
|
2024-11-02 22:11:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
|
2024-11-02 22:11:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新参数的索引
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parameterDetails"></param>
|
|
|
|
|
|
private void UpdateParamIndex(ref ParameterDetails[] parameterDetails)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < parameterDetails.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var pd = parameterDetails[i];
|
|
|
|
|
|
pd.Index = i;
|
2024-11-02 16:48:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-02 22:11:38 +08:00
|
|
|
|
|
2024-11-02 23:47:41 +08:00
|
|
|
|
|
2024-11-02 16:48:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 不包含方法信息的基础节点(后续可能要改为DLL引入基础节点)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public MethodDetails()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成元数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeModel">标识属于哪个节点</param>
|
2025-05-31 12:15:01 +08:00
|
|
|
|
public MethodDetails(IFlowNode nodeModel)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-23 19:22:27 +08:00
|
|
|
|
NodeModel = nodeModel;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从方法信息中读取
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="Info"></param>
|
|
|
|
|
|
public MethodDetails(MethodDetailsInfo Info)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Info.NodeType.TryConvertEnum<NodeType>(out var nodeType))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("无效的节点类型");
|
|
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
AssemblyName = Info.AssemblyName;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
MethodName = Info.MethodName;
|
2024-10-28 15:21:08 +08:00
|
|
|
|
MethodAnotherName = Info.MethodAnotherName;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
MethodDynamicType = nodeType;
|
|
|
|
|
|
ReturnType = Type.GetType(Info.ReturnTypeFullName);
|
|
|
|
|
|
ParameterDetailss = Info.ParameterDetailsInfos.Select(pinfo => new ParameterDetails(pinfo)).ToArray();
|
2024-11-02 22:11:38 +08:00
|
|
|
|
ParamsArgIndex = Info.IsParamsArgIndex;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 转为信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public MethodDetailsInfo ToInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new MethodDetailsInfo
|
|
|
|
|
|
{
|
2024-11-03 21:17:45 +08:00
|
|
|
|
AssemblyName = this.AssemblyName,
|
2024-11-02 16:48:40 +08:00
|
|
|
|
MethodName = this.MethodName,
|
|
|
|
|
|
MethodAnotherName = this.MethodAnotherName,
|
|
|
|
|
|
NodeType = this.MethodDynamicType.ToString(),
|
2025-05-30 23:31:31 +08:00
|
|
|
|
ParameterDetailsInfos = this.ParameterDetailss?.Select(p => p.ToInfo()).ToArray(),
|
|
|
|
|
|
ReturnTypeFullName = this.ReturnType?.FullName,
|
2024-11-02 22:11:38 +08:00
|
|
|
|
IsParamsArgIndex = this.ParamsArgIndex,
|
2025-07-07 20:40:24 +08:00
|
|
|
|
IsAsync = this.IsAsync,
|
2024-10-20 12:10:57 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-11-02 16:48:40 +08:00
|
|
|
|
/// 从DLL拖动出来时,从元数据拷贝新的实例,作为属于节点独享的方法描述
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-13 17:34:03 +08:00
|
|
|
|
public MethodDetails CloneOfNode(IFlowNode nodeModel)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-11-02 16:48:40 +08:00
|
|
|
|
// this => 是元数据
|
2025-05-31 12:15:01 +08:00
|
|
|
|
var md = new MethodDetails(nodeModel) // 创建新节点时拷贝实例
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2025-03-20 22:54:10 +08:00
|
|
|
|
AssemblyName = this.AssemblyName, // 拷贝
|
|
|
|
|
|
//ActingInstance = this.ActingInstance,
|
|
|
|
|
|
ActingInstanceType = this.ActingInstanceType, // 拷贝
|
|
|
|
|
|
MethodDynamicType = this.MethodDynamicType, // 拷贝
|
|
|
|
|
|
MethodAnotherName = this.MethodAnotherName, // 拷贝
|
|
|
|
|
|
ReturnType = this.ReturnType, // 拷贝
|
|
|
|
|
|
MethodName = this.MethodName, // 拷贝
|
|
|
|
|
|
MethodLockName = this.MethodLockName, // 拷贝
|
|
|
|
|
|
ParamsArgIndex = this.ParamsArgIndex, // 拷贝
|
2025-07-29 14:25:31 +08:00
|
|
|
|
ParameterDetailss = this.ParameterDetailss?.Select(p => p?.CloneOfModel(nodeModel)).ToArray() , // 拷贝属于节点方法的新入参描述
|
2025-07-07 20:40:24 +08:00
|
|
|
|
IsAsync = this.IsAsync, // 拷贝
|
2025-07-09 21:49:26 +08:00
|
|
|
|
IsStatic = this.IsStatic, // 拷贝
|
2024-10-20 12:10:57 +08:00
|
|
|
|
};
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
return md;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 15:21:08 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2025-05-28 23:19:00 +08:00
|
|
|
|
if (string.IsNullOrEmpty(this.MethodName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
2024-12-31 21:47:01 +08:00
|
|
|
|
var tmp = this.MethodName.Split('.') ;
|
|
|
|
|
|
var methodName = tmp[tmp.Length - 1];
|
2024-10-28 15:21:08 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-12-31 21:47:01 +08:00
|
|
|
|
sb.AppendLine($"节点Guid:");
|
|
|
|
|
|
sb.AppendLine($"\t{this.NodeModel.Guid}");
|
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
|
sb.AppendLine($"方法名称:");
|
|
|
|
|
|
sb.AppendLine($"\t{methodName}");
|
|
|
|
|
|
sb.AppendLine($"方法别名:");
|
|
|
|
|
|
sb.AppendLine($"\t{this.MethodAnotherName}");
|
|
|
|
|
|
sb.AppendLine($"需要实例:");
|
|
|
|
|
|
sb.AppendLine($"\t{this.ActingInstanceType?.FullName}");
|
2024-10-28 15:21:08 +08:00
|
|
|
|
sb.AppendLine($"");
|
2024-12-31 21:47:01 +08:00
|
|
|
|
sb.AppendLine($"入参信息:");
|
2024-11-02 16:48:40 +08:00
|
|
|
|
for (int i = 0; i < ParameterDetailss.Length; i++)
|
2024-10-28 15:21:08 +08:00
|
|
|
|
{
|
2024-11-02 16:48:40 +08:00
|
|
|
|
ParameterDetails arg = this.ParameterDetailss[i];
|
2024-11-02 22:11:38 +08:00
|
|
|
|
sb.AppendLine(arg.ToString());
|
2024-10-28 15:21:08 +08:00
|
|
|
|
}
|
2024-12-31 21:47:01 +08:00
|
|
|
|
sb.AppendLine();
|
2024-10-28 15:21:08 +08:00
|
|
|
|
sb.AppendLine($"返回值信息:");
|
2024-12-31 21:47:01 +08:00
|
|
|
|
sb.AppendLine($"\t{this.ReturnType?.FullName}");
|
2024-10-28 15:21:08 +08:00
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|