TryFromName Fails despite valid parameter

Above is my exact code at runtime. The cell I am pulling the value from has a value of "Released", and I have an enum with the name "Released". When performing a boolean check against the names, they are equivalent. Yet TryFromName returns FALSE.
/// <summary>
/// Enum describing various 'Release States' for software versions.
/// </summary>
public sealed class SoftwareReleaseState : SmartEnum<SoftwareReleaseState>
{
private SoftwareReleaseState(string name, int value) : base(name, value) { }
/// <summary>
/// Unable to locate software version in the lookup table.
/// </summary>
public static SoftwareReleaseState NoRecordOfSoftwareVersion { get; } = new(nameof(NoRecordOfSoftwareVersion), 0);
/// <summary>
/// Software is available for production / field use.
/// </summary>
public static SoftwareReleaseState Released { get; } = new(nameof(Released), 5);
}
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);
}
}
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?
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