views:

113

answers:

1

If you do ResourceManager.GetString(Key), you can get the value of an item in a resource. Is there a way to do a reverse lookup to get the key from the resource given the value (essentially de-translate)?

+4  A: 

You should be able to get the ResourceSet and iterate over it's values and return the key if they are equal. Just remember, you need to compare values, not references. Something along these lines (Not compiled and tested, but something similar)

  System.Resources.ResourceManager rm = 
     new System.Resources.ResourceManager("MyAssembly.MyResources",
         System.Reflection.Assembly.GetExecutingAssembly());
  System.Resources.ResourceSet rs = 
     rm.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, 
         false, false);
  System.Collections.IDictionaryEnumerator ide = rs.GetEnumerator();
  while(ide.MoveNext())
  {
      if (ide.Value == val)
          return ide.Key;
  }
Mystere Man
When I do this, I get back a null ResourceSet. Any ideas?
brian
I changed both the bool parameters in GetResourceSet to true and it worked. Thanks!
brian