Reverse a ResourceManager

廉价感情. 提交于 2020-01-04 03:15:15

问题


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)?


回答1:


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;
  }


来源:https://stackoverflow.com/questions/563506/reverse-a-resourcemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!