Comments (3)
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.
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.
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)
- EF Core 7 CosmosDb - Using entity with SmartEnum property causes EF Core to create entries in another container
- SmartEnum not deserialized as path parameter HOT 2
- SmartEnum<TEnum>.List does not work if fields are not defined on TEnum
- Add source generator
- Why no longer support for netstandard and previous .net versions? HOT 4
- The entity type requires a primary key to be defined... HOT 1
- Recommended way to force varchar / unicode=false on SmartEnum properties with EFCore
- How to persist SmartFlagEnum in EF Core HOT 3
- GetFlagEnumValues is not safe to use
- Could not override SmartEnum.Value
- ModelBinding sub-package not available in NuGet HOT 3
- Methods FromName() and TryFromName() for SmartFlagEnum do not work with ignore case at true
- How do i use this with NSwag / fast endpoints ? HOT 3
- Deserialization of appsettings.json via ConfigurationBuilder doesn't work HOT 1
- EF Core 7 InMemory - Throwing System.ArgumentException with any query involving a SmartEnum HOT 3
- README Example does not compile
- Case Insensitive String Enum HOT 1
- Enable Discussions for repo
- Loading SmartEnum from Entity Framework without specifying it as a static property
- Not compatible with .Net 6 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smartenum.