{"record":{"id":"0a6d9e0facbac3fb","repo":"babalae/better-genshin-impact","slug":"dimensions-ex-message","errorCode":null,"errorMessage":"创建维度为 {dimensions} 的数组失败: {ex.Message}","messagePattern":"创建维度为 (.+?) 的数组失败: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/Dependence/CustomHostFunctions.cs","lineNumber":31,"sourceCode":"    /// <param name=\"dimensions\">数组维度</param>\n    /// <returns>交错数组变量</returns>\n    public object NewVarOfArr<T>(int dimensions)\n    {\n        try\n        {\n            Type arrayType = typeof(T);\n            for (int i = 0; i < dimensions; i++)\n            {\n                arrayType = arrayType.MakeArrayType();\n            }\n\n            MethodInfo newVarMethod = typeof(HostFunctions).GetMethod(nameof(newVar))!;\n            MethodInfo genericMethod = newVarMethod.MakeGenericMethod(arrayType);\n            return genericMethod.Invoke(this, new object?[] { null })!;\n        }\n        catch (Exception ex)\n        {\n            throw new InvalidOperationException($\"创建维度为 {dimensions} 的数组失败: {ex.Message}\", ex);\n        }\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":35,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/CustomHostFunctions.cs#L13-L35","documentation":"InvalidOperationException thrown by CustomHostFunctions.NewVarOfArr<T> at line 31, wrapping any exception raised while building a jagged array type via reflection. The method repeatedly calls Type.MakeArrayType() `dimensions` times, then reflects HostFunctions.newVar to expose it to ClearScript. Failures come from: negative dimensions (MakeArrayType accepts 0+), implausibly large dimensions exhausting type-system limits, T being a type ClearScript cannot marshal, or HostFunctions.newVar not being invokable on this instance. The original error is preserved in InnerException.","triggerScenarios":"JS calls `hostFunc.NewVarOfArr(-1)` or `NewVarOfArr(1000)`. T resolves to a type the host cannot bind. The HostFunctions instance is in an invalid script context (e.g. used outside a ScriptEngine host).","commonSituations":"Script passes a user-controlled or computed dimensions value without bounds-checking. Recursive/nested generic array construction exceeding runtime limits. Type mismatch between JS expectation and the generic T inferred by ClearScript.","solutions":["Validate dimensions: require 0 <= dimensions <= ~5 (a sane cap) before calling.","Inspect `ex.InnerException` (and `InnerException.InnerException` for TargetInvocationException) to find the real cause — the outer message only restates it.","Ensure NewVarOfArr is called on a HostFunctions instance attached to a live ScriptEngine.","Confirm T is a host-exposed, marshalling-compatible type."],"exampleFix":"// before (C#)\npublic object NewVarOfArr<T>(int dimensions)\n{\n    try\n    {\n        Type arrayType = typeof(T);\n        for (int i = 0; i < dimensions; i++) arrayType = arrayType.MakeArrayType();\n        MethodInfo genericMethod = typeof(HostFunctions).GetMethod(nameof(newVar))!.MakeGenericMethod(arrayType);\n        return genericMethod.Invoke(this, new object?[] { null })!;\n    }\n    catch (Exception ex)\n    {\n        throw new InvalidOperationException($\"创建维度为 {dimensions} 的数组失败: {ex.Message}\", ex);\n    }\n}\n\n// after (C#) — validate dimensions and unwrap the real error\npublic object NewVarOfArr<T>(int dimensions)\n{\n    if (dimensions < 0 || dimensions > 5)\n        throw new ArgumentOutOfRangeException(nameof(dimensions), \"dimensions must be between 0 and 5\");\n    try\n    {\n        Type arrayType = typeof(T);\n        for (int i = 0; i < dimensions; i++) arrayType = arrayType.MakeArrayType();\n        MethodInfo genericMethod = typeof(HostFunctions).GetMethod(nameof(newVar))!.MakeGenericMethod(arrayType);\n        return genericMethod.Invoke(this, new object?[] { null })!;\n    }\n    catch (TargetInvocationException ex)\n    {\n        throw new InvalidOperationException($\"创建维度为 {dimensions} 的数组失败: {ex.InnerException?.Message}\", ex.InnerException ?? ex);\n    }\n}","handlingStrategy":"validation","validationCode":"// C# — bound the dimensions argument\nif (dimensions < 0 || dimensions > 5)\n    throw new ArgumentOutOfRangeException(nameof(dimensions), \"dimensions must be between 0 and 5\");","typeGuard":null,"tryCatchPattern":"try\n{\n    return hostFunc.NewVarOfArr<T>(dimensions);\n}\ncatch (InvalidOperationException ex)\n{\n    // The real cause is in InnerException (often TargetInvocationException)\n    var root = ex.InnerException is TargetInvocationException tie ? tie.InnerException : ex.InnerException;\n    throw new InvalidOperationException($\"NewVarOfArr failed for dim {dimensions}: {root?.Message}\", root ?? ex);\n}","preventionTips":["Bounds-check the dimensions argument (0 to a small cap) before calling.","Always inspect InnerException (unwrap TargetInvocationException) to find the real error.","Ensure NewVarOfArr is called on a HostFunctions instance attached to a live ScriptEngine.","Confirm the generic T is a host-marshallable type."],"tags":["script","clearscript","reflection","array","host-functions"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}