暂时实现了简陋的脚本AST分析解释,后面再绑定到控件上

This commit is contained in:
fengjiayi
2024-12-20 23:39:29 +08:00
parent 114e81424b
commit ef119e11e3
52 changed files with 3175 additions and 261 deletions

View File

@@ -22,22 +22,11 @@ namespace Serein.Library
public DelegateDetails(MethodInfo methodInfo)
{
var emitMethodType = EmitHelper.CreateDynamicMethod(methodInfo, out var emitDelegate);
_emitMethodType = emitMethodType;
_emitMethodInfo = emitMethodType;
_emitDelegate = emitDelegate;
}
/// <summary>
/// 记录Emit委托
/// </summary>
/// <param name="EmitMethodType"></param>
/// <param name="EmitDelegate"></param>
public DelegateDetails(EmitMethodType EmitMethodType, Delegate EmitDelegate)
{
_emitMethodType = EmitMethodType;
_emitDelegate = EmitDelegate;
}
/*/// <summary>
/// 更新委托方法
@@ -50,9 +39,13 @@ namespace Serein.Library
_emitDelegate = EmitDelegate;
}*/
private Delegate _emitDelegate;
private EmitMethodType _emitMethodType;
private EmitMethodInfo _emitMethodInfo;
/// <summary>
/// 该Emit委托的相应信息
/// </summary>
public EmitMethodInfo EmitMethodInfo => _emitMethodInfo;
///// <summary>
///// <para>普通方法Func&lt;object,object[],object&gt;</para>
@@ -65,6 +58,22 @@ namespace Serein.Library
///// </summary>
//public EmitMethodType EmitMethodType { get => _emitMethodType; }
public async Task<object> AutoInvokeAsync(object[] args)
{
if (_emitMethodInfo.IsStatic)
{
return await InvokeAsync(null, args);
}
else
{
var obj = Activator.CreateInstance(_emitMethodInfo.DeclaringType);
return await InvokeAsync(obj, args);
}
throw new Exception("Not static method");
}
/// <summary>
/// <para>使用的实例必须能够正确调用该委托,传入的参数也必须符合方法入参信息。</para>
/// </summary>
@@ -77,16 +86,20 @@ namespace Serein.Library
{
args = Array.Empty<object>();
}
if(_emitMethodInfo.IsStatic)
{
instance = null;
}
object result = null;
if (_emitMethodType == EmitMethodType.HasResultTask && _emitDelegate is Func<object, object[], Task<object>> hasResultTask)
if (_emitDelegate is Func<object, object[], Task<object>> hasResultTask)
{
result = await hasResultTask(instance, args);
}
else if (_emitMethodType == EmitMethodType.Task && _emitDelegate is Func<object, object[], Task> task)
else if (_emitDelegate is Func<object, object[], Task> task)
{
await task.Invoke(instance, args);
}
else if (_emitMethodType == EmitMethodType.Func && _emitDelegate is Func<object, object[], object> func)
else if (_emitDelegate is Func<object, object[], object> func)
{
result = func.Invoke(instance, args);
}