Oh sorry, I missed that it is in a different assembly. In that case, you will want to prefix the type string with the assembly such as:
Enum.Parse(Type.GetType("AssemblyName,Company.Product+Options"),"Two") Where AssemblyName is the the name of the assembly where the enum is located. Greg -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Gregory Miley Sent: Friday, July 14, 2006 11:01 To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: Convert string enum constant to the underlying value Try what Don suggested, but replace the "." between Product and Option with a "+" as below: int value = (int)Enum.Parse(Type.GetType("Company.Product+Options"),"Two"); Greg -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Fernando Tubio Sent: Friday, July 14, 2006 10:31 To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: Re: Convert string enum constant to the underlying value Thank you Don. I'm afraid this doesn't work. You cannot use Type.GetType unless you provide the full assembly qualified name of the enum's type and I only have the 'simple' name. This would only work if the enum were defined in the same assembly or in mscorlib.dll. That's the reason I was enumerating the loaded assemblies in order to search for a matching type. It would have to be something like this int value = (int)Enum.Parse( Type.GetType("Company.Product.Options, SomeAssembly, Version=...., Culture=..., PublicKeyToken=...."), "Two"); except that I only have "Company.Product.Options.Two". Fernando Tubio ----- Original Message ----- From: "Don Stanley" <[EMAIL PROTECTED]> Sent: Friday, July 14, 2006 11:10 AM Subject: Re: Convert string enum constant to the underlying value Try int value = (int)Enum.Parse(Type.GetType("Company.Product.Options"),"Two"); Don -----Original Message----- From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf Of Fernando Tubio Sent: Friday, July 14, 2006 8:44 AM To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM Subject: [ADVANCED-DOTNET] Convert string enum constant to the underlying value Is there anything in the .NET framework to convert a string representation of an enum constant to it's value? For example, given the following enum namespace Company.Product { public enum Options { One = 1, Two = 2, Three = 3 } } Is there any method that when given the string "Company.Product.Options.Two" as input will return the value 2 as output? Enum.Parse doesn't exactly qualify as it requires knowing the enum's type in advance. Well, it could be used if the type is first extracted from the string and searched for in the loaded assemblies. For example, int ConvertEnumToInt32 ( string expression ) { int index = expression.LastIndexOf( '.' ); if ( index > 0 ) { Type type = null; string typeName = expression.Substring( 0, index ); foreach ( Assembly assembly in AppDomain.CurrentDomain.GetAssemblies( )) { type = assembly.GetType( typeName ); if ( type != null ) { return ( int ) Enum.Parse( type, expression.Substring( index + 1 ) ); } } } throw new ArgumentOutOfRangeException( "expression", expression ); } The preceding code works but I wonder if there is a more direct route. For instance, is enumerating the loaded assemblies and calling GetType on each an appropiate mechanism to search for the enum's type? Also, I suppose the code would fail if the enum is defined in a referenced assembly which hasn't been loaded yet. Are there any alternatives? Does parsing the name of the enum's type and the constant's name by splitting the string at the last '.' character cover every possible case? Fernando Tubio =================================== This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com