Array of string to Enum types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guy, i just wondering that is it posible to convert the array of string to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show me
the way to do it
Thank you very much
Thomas
 
Hi guy, i just wondering that is it posible to convert the array of string
to
enum types dynamically, for example i get the list of countries name from
database and would like to convert all of them to Enum. If so please show
me
the way to do it
Thank you very much
Thomas

You can generate an Enum on the fly by using the methods in
System.Reflection.Emit namespace. The following code will create an Enum
with two countries.

AssemblyBuilder myAssemblyBuilder;
ModuleBuilder myModuleBuilder;
EnumBuilder myEnumBuilder;
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "MyAssembly";
myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Run);
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule");
myEnumBuilder =
myModuleBuilder.DefineEnum("MyNamespace.Countries",TypeAttributes.Public,
typeof(Int32));
FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("Norway", 1);
FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("United Kingdom",
2);
myEnumBuilder.CreateType();

You can use this code as a boiler plate for your dynamic country enum.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Assuming you have defined an Enum of countries, you can do:
Country country = (Country) Enum.Parse(typeof(Country), countryString,
true);

This will however require that the country names in the data table matches
those defined in the enum.

Another way might be to store an integer in the database representing the
country,
which is then associated with the member of the enum that uses that
particular value.
This would also be a lot more efficient.

Country country = (Country) countryInteger;

But the preferred approach really depends on how, exactly, the country
names/identifiers is stored in the database.
 
Thank you for your information however could you please also show me how to
apply this in the following situation

Assume i have the method which return the country name
[WebMethod]
public string GetCountryName(CountryType type) {}

The CountryType is the enum type which getting from country names from the
database

How can i allow user to use the CountryType that i've created on the fly.
 
Assume i have the method which return the country name
[WebMethod]
public string GetCountryName(CountryType type) {}

The CountryType is the enum type which getting from country names from the
database
You can't use dynamically created types with web services because the types
your web service returns must accept and return types defined in the WSDL
for the web service.

From what I can understand from your problem description and the code above
you want your method to accept a list of codes (e.g. ISO language codes such
as NO and GB) and return a list of full names from a database? If this is
the case, an enum is not the appropriate type to use, you should use an list
instead.
For example:
public string GetCountryName(ArrayList countries) {}

An enum is a type where a list of named constants can be defined. Unless the
enum is marked with the Flags attribute (which is used for bitwise flags) an
enum instance can only represent one constant at a time.
How can i allow user to use the CountryType that i've created on the fly.
You can create new instances of dynamically created types using bysing the
System.Activator class.

Anders Norås
http://dotnetjunkies.com/weblog/anoras
 
Back
Top