How to Get Enum By Description Values? [duplicate] - c#

This question already has answers here:
Finding an enum value by its Description Attribute [duplicate]
(3 answers)
Get enum from enum attribute
(7 answers)
Closed 8 years ago.
I have an enum width descriptions:
public enum PostTypes {
[Description("Business")]
Business = 1 << 0,
[Description("Design"]
Design = 1 << 1,
[Description("Marketing")]
Marketing = 1 << 2,
} // PostTypes
I would like to get an Enum based on Description, so:
1 - If I have String="Business" I would get PostTypes.Business.
2 - If I have String[] = new String[] { "Business", "Design" } I would have PostTypes.Business | PostTypes.Design.
UPDATE
I am using the following:
Enum.GetValues(typeof(PostTypes)).Cast<PostTypes>().Where(v => new String[] { "Business", "Design" }.Contains(v.GetDescription())).Aggregate((a, b) => a | b);
This is working even for Flag enums and string arrays.
Could someone, please, help me in creating an extension where I pass the enum type and use a func for setting the rule for search, either the Description attribute or any other.
How can I do this?
Thank You,
Miguel

Related

How can I get an enum name from the name of the enum type and an int [duplicate]

This question already has answers here:
How do I cast int to enum in C#?
(32 answers)
Convert String to Type in C# [duplicate]
(4 answers)
Enum String Name from Value
(14 answers)
Get the name of Enum value
(3 answers)
Get enum name when value is known
(7 answers)
Closed 5 years ago.
From another layer in an application I can get back the name of the enum type and an integer value. How can I use this data to get the string representation of that value for that enum.
For example, if I have an enum:
public enum Cheese
{
Edam = 1,
Cheddar = 3,
Brie = 201,
RedLeicester = 1001
}
and the data I have available is
string dataType = "Cheese";
int dataValue = 3;
How can I get the result:
"Cheddar"
In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:
string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);
If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)
should do the trick (uncompiled, on subway)
enum.Parse() would be your friend here.
Like so:
int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());

Boolean expression involving Enum and Int [duplicate]

This question already has answers here:
How to compare enum and int values?
(7 answers)
What's the simplest way to compare an enum to a integer value return from a DB
(5 answers)
C# Enum - How to Compare Value
(5 answers)
Closed 5 years ago.
I have a simple enum with a few values
Enum Status{
Available,
Taken,
Sold//and many more
}
I want to check whether the status is somewhere in between the possible values(assuming the values are written in such an order that the lowest are the beginning of a process, and the last ones are the final steps of the process)
Something like
Status s1;//Some value
if(s1<5 && s1>3)
//The value is one of the enum values in those range
//(for example the job has already been accepted, but has still not been
//shipped)
Is it possible?
Thanks.
You can assign integer values to your enum and then check.
enum Status
{
Available = 1,
Taken = 2,
Sold = 3
//and many more
}
Status s1; // any value
if ((int)s1 <= 5 && (int)s1 >= 3)

Why can't we add static methods to enums? [duplicate]

This question already has answers here:
Extension method on enumeration, not instance of enumeration
(7 answers)
Closed 8 years ago.
I wonder why we can't add static methods (only methods, not properties) into enums? Is there any explanation for that?
It would be very useful if it was allowed.
And I also want to learn who forbids us to do it? Is it IL or C#?
Edit:
I don't want to use extension methods. Because I dont need to pass an instance of that enum. I don't need it's value there...
I want to call something like FooTypes.GetGoodFoos() not something FooTypes.BadFoo.GetSomething()
Edit 2:
Is that only me who thinks this could be more useful rather than writing this method in another class?
public enum Colors
{
Red,
LightRed,
Pink,
/* .
.
. */
Green
public static Colors[] GetRedLikes()
{
return new Colors[]
{
Colors.Red,
Colors.LightRed,
Colors.Pink
}
}
}
As the other answers say, it's not possible.
I know, this does not answer your question, but I want to give an alternative to your example.
Because, basically what you try to archive is already possible with flags. So let me take your "GetRedLikes" example:
[Flags]
public enum Colors : byte
{
Transparent = 0, // = 0 (00000000)
Red = 1 << 0, // = 1 (00000001)
LightRed = 1 << 1, // = 2 (00000010)
Pink = 1 << 2, // = 4 (00000100)
Green = 1 << 3, // = 8 (00001000)
RedLikes = Colors.Red | Colors.LightRed | Colors.Pink // = 7 (00000111)
}
Then Colors.RedLikes will contain Red, LightRed and Pink. All the magic is done by bits, as always. Your condition then should look like this:
Colors c = Colors.LightRed;
if(c & Colors.RedLikes != 0)
{
// c is red-alike
}
Of course, this solution will not allow you to do very complex algorithms, it's no method type replacement. But it allows you to combine more than one enum in a set. If you need further functions, you have to build a method in an extra class.
I use static class for same cases:
public enum SomeEnum
{
Item1,
Item2,
Item3
}
public static class SomeEnumHelper
{
public static SomeEnum[] GetMainItems()
{
return new[] {SomeEnum.Item1, SomeEnum.Item2};
}
}
We can't add methods to enums because is how the language is made, and you are free to read the specification of the C# Language Specification
14.3 Enum members
The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No
two enum members can have the same name.
enum-member-declarations: enum-member-declaration
enum-member-declarations , enum-member-declaration
enum-member-declaration: attributesopt identifier attributesopt
identifier = constant-expression
Each enum member has an
associated constant value. The type of this value is the underlying
type for the containing enum. The constant value for each enum member
must be in the range of the underlying type for the enum.

How to determine the no of enum items declared in c# [duplicate]

This question already has answers here:
Total number of items defined in an enum
(10 answers)
Closed 8 years ago.
I need to know the number of enums that have been declared.
So far I am using the following which works but I wonder if there is a better way?
enum MyEnum
{
foo = 1,
bar = 2
}
int noOfEnums = Enum.GetNames(typeof(MyEnum)).Count();
noOfEnums will be 2;
You may try to use:
enum MyEnum
{
foo = 1,
bar = 2
}
var noOfEnums = Enum.GetNames(typeof(MyEnum)).Length;
The length property of this array equals the number of items defined in the enum
Also check Count Items in a C# Enum

Test that only a single bit is set in Flags Enum [duplicate]

This question already has answers here:
How do I check if more than one enum flag is set?
(5 answers)
Closed 2 years ago.
So I have a flags Enum
public Enum test
{
test1 = 1,
test2 = 2,
test3 = 4,
etc.
}
How can I test that one bit, and only one bit is set?
I've 100% done this before but my mind is not working this am!
To check that only a single bit is set in a number, the number must (by definition) be a power of two. As such, you can use the following to test:
int intVal = ((int)myEnumFlags);
bool singleBitIsSet = intVal != 0 && (intVal & (intVal-1)) == 0;
My favorite reference for this kind of thing:
http://aggregate.org/MAGIC

Categories