Giter Club home page Giter Club logo

Comments (3)

RFBomb avatar RFBomb commented on June 2, 2024

In my application I've worked around the issue by wrapping the SmartEnum class provided by this library with an identically named abstract class, so all of my enums will inherit that functionality, effectively overriding the library functionality.

    /// <inheritdoc cref="Ardalis.SmartEnum.SmartEnum{TEnum}"/>
    public abstract class SmartEnum<T> : Ardalis.SmartEnum.SmartEnum<SmartEnum<T>> where T : SmartEnum<T>
    {
        /// <inheritdoc cref="Ardalis.SmartEnum.SmartEnum{TEnum}.SmartEnum(string, int)"/>
        protected SmartEnum(string name, int value) : base(name, value) { }

        /// <inheritdoc cref="MiscExtensions.GetAll{T}"/>
        public static IEnumerable<T> GetAll() => typeof(T).GetProperties(System.Reflection.BindingFlags.Public |
                            System.Reflection.BindingFlags.Static)
                 .Select(f => f.GetValue(null))
                 .Cast<T>();

        /// <inheritdoc cref="Ardalis.SmartEnum.SmartEnum{TEnum, TValue}.TryFromName(string, bool, out TEnum)"/>
        public static bool TryFromName(string name, out T result, bool caseSensitive = false)
        {
            if (caseSensitive)
                result = GetAll().SingleOrDefault(o => o.Name.Equals(name, comparisonType: StringComparison.CurrentCultureIgnoreCase));
            else
                result = GetAll().SingleOrDefault(o => o.Name.Equals(name, comparisonType: StringComparison.CurrentCulture));
            return !(result is null);
        }

    }

from smartenum.

ardalis avatar ardalis commented on June 2, 2024
        public static bool TryFromName(string name, bool ignoreCase, out TEnum result)
        {
            if (String.IsNullOrEmpty(name))
            {
                result = default;
                return false;
            }

            if (ignoreCase)
                return _fromNameIgnoreCase.Value.TryGetValue(name, out result);
            else
                return _fromName.Value.TryGetValue(name, out result);
        }

so the question is why does your version work but the above does not?

from smartenum.

RFBomb avatar RFBomb commented on June 2, 2024

My classes are defined like so:

public class MyEnum {
    private MyEnum(string name, int value){ }

    public static MyEnum FirstValue { get; } = new ("FirstValue",0);
}

which makes them all Properties. If someone were to use ReadOnly Fields instead of Properties, your version may work, I'm not sure. But Since I declare all of mine as Properties (so that I can use visual studios 'where used' feature), my code works.

My code supplied above in my wrapper class is using reflection to grab all the properties of the specified type, which would be all my defined enums. As for why the base code doesn't work, I don't know. My guess is its 'Get-Only Property vs Read-Only Field' discrepancy, but I'm not sure if its not that

Actually, I'm relatively positive this is it.

In my other Issue #293, I noticed this block of code:

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

If the TryFromName utilizes that backer to iterate and check the values, then my classes would fail since that GetAllOptions is only returning the Fields of the specified type. I think it would work if the SelectMany didnt target Fields or Properties, but instead only checked if it was public. I know someone else was assigned that project, but I do think that getting the above method working for properties and fields, then also making it public, would likely resolve both this issue and #293

from smartenum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.