Domanda

Sto creando un metodo per convertire un enum a una corda. I nomi descrittivi sono memorizzati in un file di risorse e sono soggetti alla globalizzazione. Così ho creato due file di risorse: Enums.resx e Enums.pt-BR.resx le cui chiavi sono il nome del enum seguito dal suo valore (cioè DeliveryStatus_WaitingForPayment)

.

Questo è il codice che sto usando per caricare la risorsa e ottenere il nome descrittivo corrispondente per l'enum:

public static string EnumToString<T>(object obj)
{
      string key = String.Empty;

      Type type = typeof(T);

      key += type.Name + "_" + obj.ToString();

      Assembly assembly = Assembly.Load("EnumResources");

      string[] resourceNames = assembly.GetManifestResourceNames();

      ResourceManager = null;

      for(int i = 0; i < resourceNames.Length; i++)
      { 
           if(resourceNames[i].Contains("Enums.resources"))
           {
                rm = new ResourceManager(resourceNames[i], Assembly.GetExecutingAssembly());

                Stream resStream = assembly.GetManifestResourceStream(resourceNames[i]);

                ResourceReader reader = new ResourceReader(resStream);

                IDictionaryEnumerator dict = reader.GetEnumerator();

                while (dict.MoveNext())
                {
                     string keyToCompare = dict.Key.ToString();

                     if (keyToCompare == key)
                         return dict.Value.ToString();
                }
           }

           return obj.ToString();
      }

}

Questo metodo funziona quasi perfettamente la differenza che ignora la CurrentUICulture e restituisce sempre i valori della risorsa di default, che è, anche quando sto usando pt-BR come il mio CurrentUICulture verrà caricato il valore da Enum.resx e non Enum.pt-BR.resx.

Che cosa sto facendo di sbagliato?

È stato utile?

Soluzione

Come si scopre stavo prendendo l'approccio sbagliato a leggere il file di risorse. Non solo non ho bisogno di lavorare la mia strada attraverso un flusso che mi impediva di ottenere il risultato in base alla CurrentUICulture.

La soluzione è molto più facile di quello del mio primo tentativo:

public static string EnumToString<T>(object obj)
{
      string key = String.Empty;

      Type type = typeof(T);

      key += type.Name + "_" + obj.ToString();

      Assembly assembly = Assembly.Load("EnumResources");

      string[] resourceNames = assembly.GetManifestResourceNames();

      ResourceManager = null;

      for(int i = 0; i < resourceNames.Length; i++)
      { 
           if(resourceNames[i].Contains("Enums.resources"))
           {
                //The substring is necessary cause the ResourceManager is already expecting the '.resurces'
                rm = new ResourceManager(resourceNames[i].Substring(0, resourceNames[i].Length - 10), assembly);

                return rm.GetString(key);
           }

           return obj.ToString();
      }

}

Spero che questo aiuta chiunque cerchi qualcosa di simile in futuro!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top