TITLE!

Tag: asp.net

Iterating through an embedded resources file.

by on Dec.16, 2008, under .NET/C#, Coding

So, I decided to try a new format for handling picklists (database mappings) and decided to leave a full code based Enum model with resources files to map to proper names. From there the SQL can be generated during our build process to populate the database. Now, the issue came in when you need to go from a sane (display) value back to the ID or Enum value. In order to get a ResourceReader on an embedded resource, the following works…

         ResourceReader v_oReader = new ResourceReader(typeof(T).Assembly.GetManifestResourceStream(typeof(T).FullName + "DataSource.resources"));
         try
         {
            foreach (DictionaryEntry entry in v_oReader)
            {
               if (entry.Value.ToString().Equals(p_szDisplayValue))
               {
                  return (T)(Enum.Parse(typeof(T), entry.Key.ToString()));
               }
            }
            throw new ApplicationException(String.Format("Unable to match value for '{0}'.", p_szDisplayValue ));
         }
         finally
         {
            v_oReader.Close();
            v_oReader = null;
         }

Where T is the Enum, and this code assumes that the embedded resx file is stored at Namespace.EnumDataSource.resources

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!