Skip to content

FromEnumString return unexpected result if input is invalid #48

@FrediKats

Description

@FrediKats

FirstOrDefault return not null, but default value, so method never throw exeption:

public static T FromEnumString<T>(string value) where T : struct, Enum
{
    value.ThrowIfNull();

    var reflectionAttributeFinder = new ReflectionAttributeFinder();

    IReadOnlyDictionary<T, EnumStringValueAttribute> enumStrings = reflectionAttributeFinder.GetAttributesFromEnumValues<EnumStringValueAttribute, T>();

    KeyValuePair<T, EnumStringValueAttribute>? selectedEnumValue = enumStrings.FirstOrDefault(p => p.Value.StringValue == value);

    if (selectedEnumValue is null)
        throw new ReflectionException($"Cannot find enum values associated to string {value} in {typeof(T).Name}");

    return selectedEnumValue.Value.Key;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Ideas/Not planned

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions