mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-02 15:50:47 +08:00
1. Script项目添加了数组表达式的支持
2. EmitHelper添加了数组创建委托的构建
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Serein.Library.Utils.EmitHelper;
|
||||
|
||||
@@ -54,7 +50,11 @@ namespace Serein.Library
|
||||
/// <summary>
|
||||
/// 集合赋值
|
||||
/// </summary>
|
||||
CollectionSetter
|
||||
CollectionSetter,
|
||||
/// <summary>
|
||||
/// 数组创建
|
||||
/// </summary>
|
||||
ArrayCreate,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -172,6 +172,12 @@ namespace Serein.Library
|
||||
this.emitType = EmitType.CollectionGetter;
|
||||
collectionGetter = EmitHelper.CreateCollectionGetter(type);
|
||||
}
|
||||
else if (emitType == EmitType.ArrayCreate)
|
||||
{
|
||||
Func<int, object> func = EmitHelper.CreateArrayFactory(type);
|
||||
this.arrayCreatefunc = func;
|
||||
this.emitType = EmitType.ArrayCreate;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("错误的构建类型");
|
||||
@@ -179,8 +185,8 @@ namespace Serein.Library
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private Func<object,object, object> collectionGetter= null;
|
||||
@@ -195,6 +201,8 @@ namespace Serein.Library
|
||||
private Func<object, object[], Task> methodTask = null;
|
||||
private Func<object, object[], object> methodInvoke = null;
|
||||
|
||||
private Func<int, object> arrayCreatefunc = null;
|
||||
|
||||
|
||||
/*/// <summary>
|
||||
/// 更新委托方法
|
||||
@@ -260,10 +268,17 @@ namespace Serein.Library
|
||||
collectionSetter(instance, args[0], args[1]);
|
||||
return null;
|
||||
}
|
||||
else
|
||||
else if(emitType == EmitType.ArrayCreate)
|
||||
{
|
||||
throw new NotSupportedException("当前委托类型不支持 InvokeAsync 方法。请使用其他方法调用。");
|
||||
if(args[0] is int count)
|
||||
{
|
||||
return arrayCreatefunc(count);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
throw new NotSupportedException("当前委托类型不支持 InvokeAsync 方法。请使用其他方法调用。");
|
||||
|
||||
}
|
||||
|
||||
private async Task<object> MethodInvoke(object instance, object[] args)
|
||||
|
||||
@@ -226,10 +226,8 @@ namespace Serein.Library.Utils
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建字段 Getter 委托:Func<object, object>
|
||||
/// 构建字段 Getter 委托:Func<object, object>
|
||||
/// </summary>
|
||||
public static Func<object, object> CreateFieldGetter(FieldInfo fieldInfo)
|
||||
{
|
||||
@@ -271,7 +269,7 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建字段 Setter 委托:Action<object, object>
|
||||
/// 构建字段 Setter 委托:Action<object, object>
|
||||
/// </summary>
|
||||
public static Action<object, object> CreateFieldSetter(FieldInfo fieldInfo)
|
||||
{
|
||||
@@ -315,7 +313,7 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建属性 Getter 委托:Func<object, object>
|
||||
/// 构建属性 Getter 委托:Func<object, object>
|
||||
/// </summary>
|
||||
public static Func<object, object> CreatePropertyGetter(PropertyInfo propertyInfo)
|
||||
{
|
||||
@@ -357,7 +355,7 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建属性 Setter 委托:Action<object, object>
|
||||
/// 构建属性 Setter 委托:Action<object, object>
|
||||
/// </summary>
|
||||
public static Action<object, object> CreatePropertySetter(PropertyInfo propertyInfo)
|
||||
{
|
||||
@@ -402,9 +400,40 @@ namespace Serein.Library.Utils
|
||||
return (Action<object, object>)method.CreateDelegate(typeof(Action<object, object>));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建数组创建委托:Func<int, object[]>
|
||||
/// </summary>
|
||||
/// <param name="elementType"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
|
||||
public static Func<int, object> CreateArrayFactory(Type elementType)
|
||||
{
|
||||
if (elementType == null) throw new ArgumentNullException(nameof(elementType));
|
||||
|
||||
var arrayType = elementType.MakeArrayType();
|
||||
|
||||
var dm = new DynamicMethod(
|
||||
$"NewArray_{elementType.Name}",
|
||||
typeof(object), // 返回 object
|
||||
new[] { typeof(int) }, // 参数:length
|
||||
typeof(EmitHelper).Module,
|
||||
true);
|
||||
|
||||
var il = dm.GetILGenerator();
|
||||
|
||||
|
||||
il.Emit(OpCodes.Ldarg_0); // length
|
||||
il.Emit(OpCodes.Newarr, elementType); // new T[length]
|
||||
il.Emit(OpCodes.Ret); // 返回 T[]
|
||||
|
||||
return (Func<int, object>)dm.CreateDelegate(typeof(Func<int, object>));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建集合赋值委托:Action<object, object, object>
|
||||
/// 构建集合赋值委托:Action<object, object, object>
|
||||
/// </summary>
|
||||
/// <param name="collectionType"></param>
|
||||
/// <returns></returns>
|
||||
@@ -474,9 +503,8 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建集合获取委托:Func<object, object, object>
|
||||
/// 构建集合获取委托:Func<object, object, object>
|
||||
/// </summary>
|
||||
/// <param name="collectionType"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
@@ -173,8 +174,52 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发掘类型中的基类
|
||||
/// </summary>
|
||||
/// <param name="types"></param>
|
||||
/// <returns></returns>
|
||||
public static Type? FindCommonBaseType(Type[] types)
|
||||
{
|
||||
if (types.Length == 0)
|
||||
return null;
|
||||
|
||||
Type? baseType = types[0];
|
||||
|
||||
foreach (var type in types.Skip(1))
|
||||
{
|
||||
baseType = FindCommonBaseType(baseType, type);
|
||||
if (baseType == typeof(object))
|
||||
break;
|
||||
}
|
||||
|
||||
return baseType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找父类
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
private static Type FindCommonBaseType(Type a, Type b)
|
||||
{
|
||||
// 如果相等,直接返回
|
||||
if (a == b) return a;
|
||||
|
||||
// 向上查找父类链,找第一个 b.IsAssignableFrom(base)
|
||||
var current = a;
|
||||
while (current != null && current != typeof(object))
|
||||
{
|
||||
if (current.IsAssignableFrom(b))
|
||||
return current;
|
||||
|
||||
current = current.BaseType!;
|
||||
}
|
||||
|
||||
return typeof(object);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user