Reputation: 190
I was hoping to get some help with this. I need a function that can take in a string that is a key for a dictionary and an enum type that is has to be cast to.
The dictionary key will be a number that corresponds to an enum. I need to know how to cast the int into an enum where the enum is variable.
Here is the function as I have it written. This might be more clear than my explanation.
string GetEnum(string keyName, Enum enumType)
{
var defaultOut = "";
Model.Form.TryGetValue(keyName, out defaultOut);
if(defaultOut != ""){
return EnumDescriptionUtility.GetDescription((enumType)Convert.ToInt32(defaultOut));
}
else{
return defaultOut;
}
}
I have used most of this code before. the difference before was that enumType was hard coded to the actual enum. I want to offload all that repetition to a function.
Any help is appreciated.
Upvotes: 0
Views: 119
Reputation: 30747
I'm not 100% sure I understand your Q, though if i do, you want to cast a value to a given enum?
This is an extension method I created recently to parse a value to a given enum. The value in this case is the string name of the enum.
public static T ToEnum<T>(this string val) where T : struct
{
T t;
if (Enum.TryParse(val, out t))
return t;
return default(T);
}
And you would use it like:
public enum MyEnum{
A = 1,
B = 2,
C = 3
}
public enum MyOtherEnum{
D = 1,
E = 2,
F = 3
}
string str = "A";
MyEnum yourEnum = str.ToEnum<MyEnum>();
string str2 = "A";
MyOtherEnum yourOtherEnum = str.ToEnum<MyOtherEnum>();
Upvotes: 1
Reputation: 1135
If you just want to get a string representing the name of the enum for a given type and int, you can do the following using Enum.GetName()
public enum Letter
{
A,
B,
C
}
public enum Number
{
One,
Two,
Three
}
public static void Main(string[] args)
{
int val1 = 2;
Type type1 = typeof(Letter);
int val2 = 0;
Type type2 = typeof(Number);
var result1 = Enum.GetName(type1, val1);
var result2 = Enum.GetName(type2, val2);
Console.WriteLine("result1 {0}", result1);
Console.WriteLine("result2 {0}", result2);
Console.ReadKey();
}
If you want the actual enum, I think you have to do something similar to the other answers. It sounds like you want to cast using a Type variable:
Type type1 = typeof(Letter);
var result = (type1) val1; // wrong
And I don't think you can do that. Even with generics you will have to specify type at some point.
Upvotes: 0
Reputation: 1135
So if I understand you correctly, you know they type of enum and the integer value. You didn't include any information about your enums so I set up a simple example.
public enum Letter
{
A,
B,
C
}
public enum Number
{
One,
Two,
Three
}
So if you know the type and integer value, you can get an enum like this:
public static Enum GetEnum(Type type, int val)
{
Enum e = null;
if (type == typeof(Letter))
{
e = (Letter)val;
}
else if (type == typeof(Number))
{
e = (Number)val;
}
return e;
}
You will need to inspect its type to use it. Maybe like this:
public static string StringFromEnum(Enum e)
{
string result = null;
if (e.GetType() == typeof(Letter))
{
result = ((Letter)e).ToString();
}
else if (e.GetType() == typeof(Number))
{
result = ((Number)e).ToString();
}
return result;
}
public static void Main(string[] args)
{
int val1 = 2;
Type type1 = typeof(Letter);
int val2 = 0;
Type type2 = typeof(Number);
var result1 = GetEnum(type1, val1);
var result2 = GetEnum(type2, val2);
Console.WriteLine("result1 {0}", StringFromEnum(result1));
Console.WriteLine("result2 {0}", StringFromEnum(result2));
Console.ReadKey();
}
Upvotes: 0