How to convert an enumeration to a type

I have an enumeration like this.

public enum eTypeVar { Int, Char, Float }; 

and I want to convert it to type to do something like this:

 eTypeVar type=eTypeVar .Int; string str= eTypeVar.ToString .ToLower(); (str as type) a=1; 

How can I do it?

0
string enums c # types
source share
3 answers

I think you need:

 Dictionary<eTypeVar, Type> _Types = new Dictionary<eTypeVar, Type> { { eTypeVar.Int, typeof(Int32) }, { eTypeVar.Char, typeof(Char) }, { eTypeVar.Float, typeof(Single) } }; public Boolean Check(eTypeVar type, Object value) { return value.GetType() == _Types[type]; } 

You cannot convert a variable to a type for another variable declaration. You need to rethink your design. In any case, it does not do what you do. If you know you want to use int, why not declare it:

 String name = "int"; int value = 1; 

If you want to have dynamic code for some reason, you can use Reflection and the general method.

 public void DoSomething<T>(T value) { .... } 

Then you can build the method at runtime using reflection and invoke it. But at this time, I think you need more C # bases to work with these functions.

0
source share

You can use Enum.Parse , for example:

 YourEnumType realValue = Enum.Parse(typeof(YourEnumType), "int"); 
+3
source share

you can try this ... for example, you listed like this

  public enum Emloyee { None = 0, Manager = 1, Admin = 2, Operator = 3 } 

then convert enum to this

 Emloyee role = Emloyee.Manager; int roleInterger = (int)role; 

end to enumerate to string ..

 Emloyee role = Emloyee.Manager; string roleString = role.ToString(); 
0
source share

All Articles