mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
1. Script项目添加了数组表达式的支持
2. EmitHelper添加了数组创建委托的构建
This commit is contained in:
@@ -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