Skip to content

FromEnumString return unexpected result if input is invalid #48

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;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions