refactor(temp) : 修复了一些命名和构造函数入参类型

This commit is contained in:
fengjiayi
2026-01-27 17:36:16 +08:00
parent 0e2e5e6a68
commit 2d136a6b2c
10 changed files with 98 additions and 41 deletions

View File

@@ -64,6 +64,11 @@ namespace Serein.Library.Utils
TaskHasResult,
}
static Task<object> ConvertTaskResult<T>(Task<T> task) where T : class
{
return task.ContinueWith(t => (object)t.Result);
}
/// <summary>
/// 判断一个类型是否为泛型 Task&lt;T&gt; 或 Task并返回泛型参数类型如果有的话
/// </summary>
@@ -100,11 +105,6 @@ namespace Serein.Library.Utils
/// <param name="methodInfo"></param>
/// <param name="delegate"></param>
/// <returns></returns>
/// <summary>
/// 根据方法信息创建动态调用的委托,返回方法类型,以及传出一个委托
/// </summary>
/// <param name="methodInfo"></param>
/// <param name="delegate"></param>
/// <returns></returns>
public static EmitMethodInfo CreateMethod(MethodInfo methodInfo, out Delegate @delegate)
{
@@ -205,6 +205,17 @@ namespace Serein.Library.Utils
// ==============================
il.Emit(isStatic ? OpCodes.Call : OpCodes.Callvirt, methodInfo);
// 如果是泛型Task
if (isTaskGeneric && taskResultType is not null)
{
var convertMethod = typeof(EmitHelper)
.GetMethod(nameof(ConvertTaskResult),
BindingFlags.Static | BindingFlags.NonPublic)!
.MakeGenericMethod(taskResultType);
il.Emit(OpCodes.Call, convertMethod);
}
// ==============================
// 6. 回写 ref / out 参数
// ==============================
@@ -237,6 +248,8 @@ namespace Serein.Library.Utils
il.Emit(OpCodes.Box, methodInfo.ReturnType);
}
il.Emit(OpCodes.Ret);
// ==============================

View File

@@ -17,7 +17,10 @@ namespace Serein.Library
/// </summary>
public static class SereinEnv
{
private static IFlowEnvironment environment;
/// <summary>
/// 运行环境
/// </summary>
public static IFlowEnvironment Environment { get;private set; }
#region 使
/// <summary>
@@ -112,7 +115,7 @@ namespace Serein.Library
{
if (environment != null)
{
SereinEnv.environment = environment;
SereinEnv.Environment = environment;
}
}
@@ -126,7 +129,7 @@ namespace Serein.Library
{
Debug.WriteLine($"{type} : {message}");
Console.WriteLine($"{type} : {message}");
SereinEnv.environment?.WriteLine(type,message,@class);
SereinEnv.Environment?.WriteLine(type,message,@class);
}
/// <summary>
@@ -139,12 +142,12 @@ namespace Serein.Library
if(@class == InfoClass.Debug)
{
SereinEnv.environment.WriteLine(InfoType.ERROR, ex.ToString(), @class);
SereinEnv.Environment.WriteLine(InfoType.ERROR, ex.ToString(), @class);
}
else
{
SereinEnv.environment.WriteLine(InfoType.ERROR, ex.Message, @class);
SereinEnv.Environment.WriteLine(InfoType.ERROR, ex.Message, @class);
}
}
@@ -157,13 +160,13 @@ namespace Serein.Library
/// <returns></returns>
public async static Task TriggerEvent(Action action)
{
if (environment is null)
if (Environment is null)
{
action?.Invoke();
}
else
{
var uco = environment.UIContextOperation;
var uco = Environment.UIContextOperation;
if (uco is null)
{
action?.Invoke();

View File

@@ -510,13 +510,15 @@ namespace Serein.Library.Utils
{
return null;
}
else
{
// 没有显示指定构造函数入参,选择参数最多的构造函数
//var constructor = GetConstructorWithMostParameters(type);
var constructors = GetConstructor(type); // 获取构造函数
if(constructors.Length == 0)
{
return null;
}
foreach(var constructor in constructors)
{
var parameters = constructor.GetParameters();
@@ -534,8 +536,10 @@ namespace Serein.Library.Utils
argObj = CreateInstance(fullName);
if (argObj is null)
{
SereinEnv.WriteLine(InfoType.WARN, "构造参数创建失败");
continue;
SereinEnv.WriteLine(InfoType.WARN, "构造参数创建失败");
argObj = CreateInstance(fullName);
throw new Exception("构造参数创建失败");
}
}
args[i] = argObj;
@@ -545,7 +549,7 @@ namespace Serein.Library.Utils
instance = Activator.CreateInstance(type, args);
if(instance != null)
{
break;
break; // 构建完成退出
}
}
catch (Exception)
@@ -553,10 +557,7 @@ namespace Serein.Library.Utils
continue;
}
}
}
InjectDependencies(instance); // 完成创建后注入实例需要的特性依赖项
_dependencies[typeName] = instance;
return instance;
@@ -662,7 +663,7 @@ namespace Serein.Library.Utils
/// </summary>
/// <param name="instance">实例</param>
/// <param name="isRecord">未完成依赖项注入时是否记录</param>
private bool InjectDependencies(object instance,bool isRecord = true)
private bool InjectDependencies(object instance, bool isRecord = true)
{
var properties = instance.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToArray()