Enumerated type names by using just a string,

B

Brian

I can get the names of an enumerated type by using

string [] strEnumeratedTypeNames =
System.Enum.GetNames(typeof(MyEnumeratedType));

But, what if I don't know what MyEnumeratedType will really be at
runtime?
Can I get the list of names by just giving a string that has the name
of a enumerated type?

Basicaly I'm trying to get a list of enumerated type names by using
the string name that exists in a different enumerated type.

I'd like to do this without using System.Reflection.
 
A

Andreas Håkansson

Brian,

Why without reflection? Reflection would be your bestfriend if you
dynamicly would want to creáte objects of a type for whích you have
the string name? But then again, you could take the static method
Type.GetType(string typeName); for a spin if you want, but 10 to 1
it uses reflection internally ;)

HTH,

//Andreas
 
G

Guest

I don't quite understand your statment about not wanting to use the System.Reflection namespace. Discovering types by name *is* reflection

It's a bit like saying you want to write to a file without using System.I
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

You can't do this without System.Reflection. Basically, you can get the
type of your enumerated type using the static GetType method on the Type
class. Once you have that, you can call the static GetNames method, using
the type that was returned as a result from the call to GetType.

Hope this helps.
 
C

Chad Myers

Brian said:
I can get the names of an enumerated type by using

string [] strEnumeratedTypeNames =
System.Enum.GetNames(typeof(MyEnumeratedType));

But, what if I don't know what MyEnumeratedType will really be at
runtime?
Can I get the list of names by just giving a string that has the name
of a enumerated type?

Basicaly I'm trying to get a list of enumerated type names by using
the string name that exists in a different enumerated type.

I'd like to do this without using System.Reflection.

So let me try to understand. You don't know which enum type it is, but
you'll be given a string like "MyEnumeratedType", right?

That's easy:

// untested code, but it's pretty close, I'm sure
string[] valueNames = Enum.GetNames(Type.GetType("MyEnumeratedType"));

You might need to fully-qualify the type name (i.e.
YourCo.SomeProduct.MyEnumeratedType).

-c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top