Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FromEnumString return unexpected result if input is invalid #48

Open
FrediKats opened this issue Jun 9, 2024 · 0 comments
Open

FromEnumString return unexpected result if input is invalid #48

FrediKats opened this issue Jun 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@FrediKats
Copy link
Member

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;
}
@FrediKats FrediKats added the bug Something isn't working label Jun 9, 2024
@github-project-automation github-project-automation bot moved this to Ideas/Not planned in Kysect task management Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Ideas/Not planned
Development

No branches or pull requests

1 participant