feat(JS): 支持调用 C#中含out/ref参数的方法 (#2884)

* feat(JS): 支持调用 C#中含out/ref参数的方法

* 新增NewVarOfArr方法用于支持交错数组

* 多删了一行
This commit is contained in:
DarkFlameMaster
2026-03-09 23:42:44 +08:00
committed by GitHub
parent 864efb42a3
commit 5a5b77266e
2 changed files with 35 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using Microsoft.ClearScript;
using System;
using System.Reflection;
namespace BetterGenshinImpact.Core.Script.Dependence;
public class CustomHostFunctions : HostFunctions
{
/// <summary>
/// 创建指定维度的交错数组变量
/// </summary>
/// <typeparam name="T">数组元素类型</typeparam>
/// <param name="dimensions">数组维度</param>
/// <returns>交错数组变量</returns>
public object NewVarOfArr<T>(int dimensions)
{
try
{
Type arrayType = typeof(T);
for (int i = 0; i < dimensions; i++)
{
arrayType = arrayType.MakeArrayType();
}
MethodInfo newVarMethod = typeof(HostFunctions).GetMethod(nameof(newVar))!;
MethodInfo genericMethod = newVarMethod.MakeGenericMethod(arrayType);
return genericMethod.Invoke(this, new object?[] { null })!;
}
catch (Exception ex)
{
throw new InvalidOperationException($"创建维度为 {dimensions} 的数组失败: {ex.Message}", ex);
}
}
}

View File

@@ -87,6 +87,7 @@ public class EngineExtend
engine.AddHostType("BvLocator", typeof(BvLocator));
engine.AddHostType("BvImage", typeof(BvImage));
engine.AddHostObject("host", new CustomHostFunctions());
// 导入 JavaScript 模块
// https://microsoft.github.io/ClearScript/2023/01/24/module-interop.html