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

SmartEnum<TEnum>.List does not work if fields are not defined on TEnum #349

Open
prlcutting opened this issue Dec 19, 2022 · 0 comments
Open

Comments

@prlcutting
Copy link

The usual usage of SmartEnum<TEnum> is like this I believe:

public class Foo : SmartEnum<Foo> { }

For some rather esoteric reasons, I'm trying to use it like this:

public class Foo
{
    public class Bar: SmartEnum<Foo> { }
}

Note that the class deriving from SmartEnum<TEnum> is different than the type TEnum in SmartEnum<TEnum> (Bar vs. Foo). Is this a supported scenario?

The reason I'm trying to make the second option work is to avoid a naming collision. My class (Foo in the example) has a property with the same name as one of the enumerated options that I want to expose as a static field. Obviously the names collide. One idea to avoid the name collision was to introduce a nested class that defines the enumeration options. In other words, usage would like this:

var someValue = Foo.Bar.SomeValue;

...rather than the more usual:

var someValue = Foo.SomeValue;

I've found that the SmartEnum<TEnum>.List property doesn't work in this scenario. List is ultimately populated with a call to GetAllOptions(), which is defined like this:

private static TEnum[] GetAllOptions()
{
    Type baseType = typeof(TEnum);

    return Assembly.GetAssembly(baseType)
        .GetTypes()
        .Where(t => baseType.IsAssignableFrom(t))
        .SelectMany(t => t.GetFieldsOfType<TEnum>())
        .OrderBy(t => t.Name)
        .ToArray();
}

I wonder, would it be possible to change this implementation in a backwards compatible manner such that the "normal" scenario would continue to work as before, while also supporting my "new" scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant