using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Entity;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Reflection;
namespace Serein.NodeFlow.Tool;
public static class MethodDetailsHelperTmp
{
///
/// 生成方法信息
///
///
///
///
public static List GetList(Type type)
{
var methodDetailsDictionary = new List();
var assemblyName = type.Assembly.GetName().Name;
var methods = GetMethodsToProcess(type);
foreach (var method in methods)
{
var methodDetails = CreateMethodDetails(type, method, assemblyName);
methodDetailsDictionary.Add(methodDetails);
}
return methodDetailsDictionary.OrderBy(it => it.MethodName).ToList();
}
///
/// 获取处理方法
///
private static IEnumerable GetMethodsToProcess(Type type)
{
return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(m => m.GetCustomAttribute()?.Scan == true);
}
///
/// 创建方法信息
///
///
private static MethodDetails CreateMethodDetails(Type type, MethodInfo method, string assemblyName)
{
var methodName = method.Name;
var attribute = method.GetCustomAttribute();
if(attribute is null)
{
return null;
}
var explicitDataOfParameters = GetExplicitDataOfParameters(method.GetParameters());
// 生成委托
var methodDelegate = GenerateMethodDelegate(type, // 方法所在的对象类型
method, // 方法信息
method.GetParameters(),// 方法参数
method.ReturnType);// 返回值
Type returnType;
if (attribute?.MethodDynamicType == Library.Enums.NodeType.Flipflop)
{
// 触发器节点
returnType = attribute.ReturnType;
}
else
{
returnType = method.ReturnType;
}
var dllTypeName = $"{assemblyName}.{type.Name}";
// object instance = Activator.CreateInstance(type);
var dllTypeMethodName = $"{assemblyName}.{type.Name}.{method.Name}";
return new MethodDetails
{
ActingInstanceType = type,
// ActingInstance = instance,
MethodName = dllTypeMethodName,
MethodDelegate = methodDelegate,
MethodDynamicType = attribute.MethodDynamicType,
MethodLockName = attribute.LockName,
MethodTips = attribute.MethodTips,
ExplicitDatas = explicitDataOfParameters,
ReturnType = returnType,
};
}
private static ConcurrentDictionary ConvertorInstance =[];
///
/// 获取参数信息
///
///
///
private static ExplicitData[] GetExplicitDataOfParameters(ParameterInfo[] parameters)
{
return parameters.Select((it, index) =>
{
Type paremType;
if (it.GetCustomAttribute() is EnumTypeConvertorAttribute attribute1 && attribute1 is not null)
{
// 存在类型选择器
paremType = attribute1.EnumType;
return GetExplicitDataOfParameter(it, index, paremType, true);
}
else if (it.GetCustomAttribute() is BindConvertorAttribute attribute2 && attribute2 is not null)
{
paremType = attribute2.EnumType;
string key = typeof(IEnumConvertor<,>).FullName + attribute2.EnumType.FullName + attribute2.ConvertorType.FullName;
if (!ConvertorInstance.ContainsKey(key))
{
Type enumConvertorType = typeof(IEnumConvertor<,>);
// 定义具体类型
Type specificType = enumConvertorType.MakeGenericType(attribute2.EnumType, it.ParameterType);
// 获取实现类的类型
Type implementorType = attribute2.ConvertorType;
// 创建实现类的实例
object instance = Activator.CreateInstance(implementorType);
// 调用 Convert 方法
MethodInfo convertMethod = implementorType.GetMethod("Convertor");
ConvertorInstance[key] = (instance, convertMethod);
}
Func