重写了Script的解释器代码,使其更加直观。重写了流程控制的部分代码,分离运行环境IOC与流程IOC。

This commit is contained in:
fengjiayi
2025-07-18 22:45:06 +08:00
parent 88de5a21f5
commit fc05cd662b
38 changed files with 567 additions and 1418 deletions

View File

@@ -128,6 +128,7 @@ namespace Serein.Library.Utils
total += ms;
Console.WriteLine($"运行1次耗时 :{total} 毫秒:");
Debug.WriteLine($"运行1次耗时 :{total} 毫秒:");
return result;
}
}

View File

@@ -17,15 +17,26 @@ namespace Serein.Library.Utils
public class EmitMethodInfo
{
/// <summary>
/// 方法声明类型
/// </summary>
public Type DeclaringType { get; set; }
/// <summary>
/// 方法类型
/// </summary>
public EmitMethodType EmitMethodType { get; set; }
/// <summary>
/// 是异步方法
/// </summary>
public bool IsTask { get; set; }
public bool IsAsync { get; set; }
/// <summary>
/// 是静态的
/// </summary>
public bool IsStatic { get; set; }
}
public enum EmitMethodType
@@ -41,16 +52,7 @@ namespace Serein.Library.Utils
/// <summary>
/// 有返回值的异步方法
/// </summary>
HasResultTask,
/// <summary>
/// 普通的方法。如果方法返回void时将会返回null。
/// </summary>
StaticFunc,
/// <summary>
/// 无返回值的异步方法
/// </summary>
StaticTask,
TaskHasResult,
}
public static bool IsGenericTask(Type returnType, out Type taskResult)
@@ -179,26 +181,31 @@ namespace Serein.Library.Utils
}
// 处理返回值如果没有返回值则返回null
il.Emit(OpCodes.Ret); // 返回
EmitMethodType emitMethodType;
if (IsTask)
{
if (IsTaskGenerics)
{
emitMethodType = EmitMethodType.TaskHasResult;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], Task<object>>));
}
else
{
emitMethodType = EmitMethodType.Task;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], Task>));
}
}
else
{
emitMethodType = EmitMethodType.Func;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], object>));
}
return new EmitMethodInfo
{
EmitMethodType = emitMethodType,
DeclaringType = methodInfo.DeclaringType,
IsTask = IsTask,
IsAsync = IsTask,
IsStatic = isStatic
};
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Serein.Library.Utils
{
/// <summary>
/// 对于 linq 的异步扩展方法
/// </summary>
public static class LinqAsyncHelper
{
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, Task<TResult>> method)
{
return await Task.WhenAll(source.Select(async s => await method(s)));
}
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, Task<TResult>> method,
int concurrency = int.MaxValue)
{
var semaphore = new SemaphoreSlim(concurrency);
try
{
return await Task.WhenAll(source.Select(async s =>
{
try
{
await semaphore.WaitAsync();
return await method(s);
}
finally
{
semaphore.Release();
}
}));
}
finally
{
semaphore.Dispose();
}
}
}
}

View File

@@ -12,7 +12,7 @@ namespace Serein.Library.Utils
{
/// <summary>
/// 一个轻量级的IOC容器
/// 一个轻量级的单例IOC容器
/// </summary>
public class SereinIOC : ISereinIOC
{
@@ -45,7 +45,7 @@ namespace Serein.Library.Utils
public event IOCMembersChangedHandler OnIOCMembersChanged;
/// <summary>
/// 一个轻量级的IOC容器
/// 一个轻量级的D单例IOC容器
/// </summary>
public SereinIOC()
{