babalae/better-genshin-impact · warning · ArgumentException

无法找到描述为 '{description}' 的枚举值

Error message

无法找到描述为 '{description}' 的枚举值

What it means

Thrown by MapTypesExtensions.ParseFromDescription when no enum field's [Description] attribute matches the input string exactly. The method reflects over typeof(MapTypes).GetFields() and compares attribute.Description == description with ordinal equality (case-sensitive). If none match, it throws ArgumentException. Used to convert a human-readable map name (e.g. '提瓦特大陆') back to the MapTypes enum.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Map/Maps/Base/MapTypes.cs:43

    [Description("霜月")]
    MoonCanon,
}
public static class MapTypesExtensions
{
    public static MapTypes ParseFromDescription(string description)
    {
        foreach (var field in typeof(MapTypes).GetFields())
        {
            if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
            {
                if (attribute.Description == description)
                {
                    return (MapTypes)field.GetValue(null)!;
                }
            }
        }
        throw new ArgumentException($"无法找到描述为 '{description}' 的枚举值", nameof(description));
    }
    
    public static MapTypes ParseFromName(string name)
    {
        if (Enum.TryParse<MapTypes>(name, true, out var result))
        {
            return result;
        }
        throw new ArgumentException($"无法找到名称为 '{name}' 的枚举值", nameof(name));
    }
}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Trim and normalize the input string before parsing; compare the value against the known descriptions in MapTypes.cs.
  2. Update the config label to exactly match a [Description] attribute value.
  3. If a description changed, update the attribute or add a compatibility alias.
  4. Consider case-insensitive or trimmed matching in ParseFromDescription if labels are user-supplied.

Example fix

// before — exact match only
if (attribute.Description == description) { ... }

// after — tolerant of whitespace/casing
if (string.Equals(attribute.Description, description?.Trim(), StringComparison.OrdinalIgnoreCase)) { ... }
Defensive patterns

Strategy: validation

Validate before calling

var known = typeof(MapTypes).GetFields()
    .Select(f => Attribute.GetCustomAttribute(f, typeof(DescriptionAttribute)) as DescriptionAttribute)
    .Where(a => a != null).Select(a => a!.Description).ToHashSet();
if (!known.Contains(description?.Trim())) throw new ArgumentException($"未知的地图描述: {description}");

Type guard

static bool IsValidMapDescription(string? d) => typeof(MapTypes).GetFields()
    .Any(f => Attribute.GetCustomAttribute(f, typeof(DescriptionAttribute)) is DescriptionAttribute a && a.Description == d);

Prevention

When it happens

Trigger: ParseFromDescription is called with a string that doesn't exactly equal any [Description(...)] value. Trailing whitespace, different casing, a translated/renamed label, or a description for a member that was removed will all fail the exact-match comparison.

Common situations: Game/config label was localized differently or updated; user typed a map name with extra spaces or full-width characters; the description string in config doesn't match the attribute text byte-for-byte; a previous enum member (and its description) was deleted but old config still references it.

Related errors


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