-1

This is my declaration.

public enum Egypt {
    Cairo = (long)1521894,
    Alexandria = (long)1522006,
    Giza = (long)1521643
}

From aspx page I get the value Egypt as a query-string. With this value I want to parse the names and values of this enumeration Egypt.

var values = Enum.GetValues(typeof(Egypt)).Cast<Egypt>();
foreach (Egypt in values)
{
    long woeid = ((long)(Egypt)(s));
}

I have the code to get the values like this but my point is the value enum - Egypt is itself determined at runtime. How will I achieve it?

Masoud
  • 8,020
  • 12
  • 62
  • 123
m Raj
  • 287
  • 1
  • 2
  • 8
  • 4
    Please search before asking a question. Simply searching for the *title* of your question finds the right answer. – Jon Skeet Mar 02 '15 at 10:49
  • 1
    You have a point but that is different from this question. I have gone through that question. Both are different. The link you pointed points to the values in the enum object and my question points to the enum object. You can check them. – m Raj Mar 02 '15 at 10:53
  • 3
    Ah, yes, I see. Reopened. It sounds like you may well just want `Type.GetType()`... although it's not clear where `City` comes in though, or why you're casting to `long` all over the place when the underlying type of `Egypt` is `int`... or what `s` is. It's entirely possible that you don't want enums at all, and just dictionaries instead. – Jon Skeet Mar 02 '15 at 11:00
  • You want something like `typeof(Egypt).Assembly.GetType("YourNamespace.Something." + enumName)` and pass that to `Enum.GetValues`. I appreciate `typeof(Egypt)` is a bit chicken-and-the-egg but I don't know which assembly/namespace your enums will be in. – ta.speot.is Mar 02 '15 at 11:03
  • Its my mistake its not city it has to be Egypt. I have corrected – m Raj Mar 02 '15 at 11:05
  • The type is itself determined at runtime. – m Raj Mar 02 '15 at 11:06

1 Answers1

4

You can use reflection to find the type:

var vals = Enum.GetValues(Type.GetType("Egypt"));

Fiddle: https://dotnetfiddle.net/AxlFW7

Of course, you'd need to add the correct namespace and/or find it in the required assembly and check for possible errors

Jcl
  • 27,696
  • 5
  • 61
  • 92