babalae/better-genshin-impact · error · ArgumentException

角色名参数必须是字符串集合或 JS Array。

Error message

角色名参数必须是字符串集合或 JS Array。

What it means

Thrown by ParseCharacterNames when the characterNames argument is not null, not a string, but also does not implement IEnumerable. The method only accepts string collections (or JS Arrays which implement IList/IEnumerable). Any other type (number, object, boolean) triggers this.

Source

Thrown at BetterGenshinImpact/GameTask/CharacterDevelopment/CharacterDevelopmentTask.cs:127

    public async Task<List<CharacterDevelopmentResult>> GetMultiCharacters(object characterNames, string? categories = null)
    {
        var names = ParseCharacterNames(characterNames);
        var categoryFlags = ParseCategories(categories);
        return await new CharacterDevelopmentStateMachineTask(names, categoryFlags)
            .Start(CancellationContext.Instance.Cts.Token);
    }

    internal static List<string> ParseCharacterNames(object characterNames)
    {
        ArgumentNullException.ThrowIfNull(characterNames);
        if (characterNames is string)
        {
            throw new ArgumentException("多角色接口需要字符串集合或 JS Array,单个字符串请使用 GetCharacter。", nameof(characterNames));
        }

        if (characterNames is not IEnumerable enumerable)
        {
            throw new ArgumentException("角色名参数必须是字符串集合或 JS Array。", nameof(characterNames));
        }

        List<string> names = [];
        foreach (var item in enumerable)
        {
            if (item is not string name)
            {
                throw new ArgumentException("角色名集合中的每个元素都必须是字符串。", nameof(characterNames));
            }

            names.Add(NormalizeCharacterName(name));
        }

        if (names.Count == 0)
        {
            throw new ArgumentException("角色名集合不能为空。", nameof(characterNames));
        }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Pass a string array or List<string> from C#, or a JS Array of strings from JavaScript.
  2. Ensure the value is iterable and contains string elements.
  3. Validate the argument type before calling GetMultiCharacters.

Example fix

// before (wrong)
await dev.GetMultiCharacters({ name: '钟离' });
// after
await dev.GetMultiCharacters(['钟离']);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the argument is an enumerable collection
if (characterNames is not string && characterNames is not IEnumerable)
{
    throw new ArgumentException("characterNames must be a string collection or JS Array.");
}

Type guard

static bool IsValidCharacterNamesArgument(object obj)
{
    return obj is null || (obj is not string && obj is IEnumerable);
}

Try / catch

try
{
    await dev.GetMultiCharacters(names);
}
catch (ArgumentException ex) when (ex.Message.Contains("必须是字符串集合"))
{
    // Convert to a proper array/collection
}

Prevention

When it happens

Trigger: Called from GetMultiCharacters(characterNames, categories) → ParseCharacterNames. characterNames is a non-null, non-string object that is not IEnumerable — e.g. a number, a plain object, or a boolean.

Common situations: JavaScript caller passes a non-array object (e.g. {name: '钟离'}) or a primitive; C# caller passes an incompatible type like an int or custom class not implementing IEnumerable.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/c6728bdc2cf74887. Report an issue: GitHub.