clausstolz / smartenums Goto Github PK
View Code? Open in Web Editor NEWMake your enums more powerful just using .Net open source implementation
Make your enums more powerful just using .Net open source implementation
I have this enum (see below) and I need a way to lookup the enum from the Code's value so I can use the enum to get the Cluster's value. It would be nice if this was a builtin feature like FindByTag
.
I've found that I can do it like this:
return SmartEnum.GetEnumerator<EventType>().Single(x => x.GetValueOf<string>("Code") == code);
but it would be nice to have a simpler syntax that is an extension method (again like FindByTag
).
public enum EventType
{
[EnumValue("Code", "T")]
[EnumValue("Cluster", 1)]
Trade,
[EnumValue("Code", "Q")]
[EnumValue("Cluster", 1)]
Quote,
[EnumValue("Code", "A")]
[EnumValue("Cluster", 2)]
AggregatePerSecond,
[EnumValue("Code", "AM")]
[EnumValue("Cluster", 2)]
AggregatePerMinute,
[EnumValue("Code", "I")]
[EnumValue("Cluster", 2)]
Imbalance,
[EnumValue("Code", "Status")]
[EnumValue("Cluster", 0)]
Status,
}
Add feature to enumerate enums.
Now versions in EnumValueAttribute
can be null and sometimes it can throw null exception. Need to fix it.
There are summary docs for main methods in project but when build source to package in new version has not summary.
Given these enums I would like to be able to do the following but I can't:
var eventTypesCluster2 = SmartEnum.GetEnumerator<EventType>().Where(x => x.GetValueOf<Cluster>("Cluster") == Cluster.Two).ToList();
The goal here as you can see is to get a subset of valid enum values for a given situation but if just one enum value does not have the EnumValue with the specified key the whole enumeration fails??? That makes no sense!
I can demonstrate that issue more clearly like this:
var test = EventType.Status.GetValueOf<Cluster>("Cluster");
My expectation would be that this should return null
since the EnumValue doesn't exist.
but I could kinda understand your apprehension there since it doesn't really make sense for non-nullable types like enum Cluster
in this case.
A possible solution would be to keep allowing GetValueOf<>
to throw the FieldNotImplementException
for non-nullable types but to return null for nullable types. With that solution by adding the ?
now any variable can be nullable and would return null rather than the FieldNotImplementException
:
var test = EventType.Status.GetValueOf<Cluster?>("Cluster");
If you're not so keen on my previous suggestion hopefully you'll like this potential solution because it should feel familiar. Make a TryGetValueOf<>
method.
An example might look like this:
var test = EventType.Status.TryGetValueOf<Cluster>("Cluster", out var clusterValue);
In my opinion I would say you should strongly consider implementing both options because being able to enumerate the enum without throwing an exception should be valuable in many cases.
public enum Cluster
{
Unknown = -1,
Zero = 0,
One = 1,
Two = 2
}
public enum EventType
{
[EnumValue("Code", "T")]
[EnumValue("Cluster", Cluster.One)]
Trade,
[EnumValue("Code", "Q")]
[EnumValue("Cluster", Cluster.One)]
Quote,
[EnumValue("Code", "A")]
[EnumValue("Cluster", Cluster.Two)]
AggregatePerSecond,
[EnumValue("Code", "AM")]
[EnumValue("Cluster", Cluster.Two)]
AggregatePerMinute,
[EnumValue("Code", "I")]
[EnumValue("Cluster", Cluster.Two)]
Imbalance,
[EnumValue("Code", "Status")]
Status,
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.