Getting translated string using reflection and Resource Manager I wrote the following code to get the translated string from Resource Manager. The method GetTranslatedString is called more than 50 times in my project.

  public static string GetTranslatedString(string input, string culture)
    {
        if (string.IsNullOrEmpty(input)) return input;
        string retInput = input;
        CultureInfo cultureInfo = new CultureInfo(culture);
        ResourceManager rm = new ResourceManager("Globalization.XYZ", Assembly.GetExecutingAssembly());

        var entry = rm.GetResourceSet(cultureInfo, true, true)
            .OfType&lt;DictionaryEntry&gt;()
            .FirstOrDefault(e =&gt; e.Key.ToString().Equals(input, StringComparison.InvariantCultureIgnoreCase));
        if (entry.Value == null) return retInput;
        return entry.Value.ToString();

    }


But I feel it's not perfect code because the ResourceManager class gets new instances many times and since I'm calling GetTranslatedString many times I am sure it will impact on performance. 

Second major problem is, I am using reflection inside this code. and it's a costly operation. 

It would helpful if I could get review on this.
public static string GetTranslatedString(string input, string culture)
{
    if (string.IsNullOrEmpty(input)) return input;
    string retInput = input;
    CultureInfo cultureInfo = new CultureInfo(culture);
    ResourceManager rm = new ResourceManager("Tesla.TSS.Globalization.SchedulingSystem", Assembly.GetExecutingAssembly());

    var entry = rm.GetResourceSet(cultureInfo, true, true)
        .OfType&lt;DictionaryEntry&gt;()
        .FirstOrDefault(e =&gt; e.Key.ToString().Equals(input, StringComparison.InvariantCultureIgnoreCase));
    if (entry.Value == null) return retInput;
    return entry.Value.ToString();

}  



Because the resources of your application won't change during runtime, the easiest way would be to once get the whole items and store them in a dictionary. Then your application can use the dictionary to retrieve the translated strings.  

I would like to suggest to make this a little bit more flexible, so you could use it in other projects too.  

At first the method which will retrieve the Dictionary&lt;string,string&gt; from the ResourceManager  

public static Dictionary&lt;string, string&gt; GetTranslations(CultureInfo cultureInfo, string baseName, Assembly assembly)
{
    ResourceManager rm = new ResourceManager(baseName, assembly);

    using (ResourceSet rs = rm.GetResourceSet(cultureInfo, true, true))
    {
        return rs.OfType&lt;DictionaryEntry&gt;()
                 .ToDictionary(r =&gt; r.Key.ToString(),
                      r =&gt; r.Value.ToString());
    }
}


as you can see, this can be called independent from your current project.  

Now let us add an overloaded method which takes the culture as a string like so  

public static Dictionary&lt;string, string&gt; GetTranslations(string culture, string baseName, Assembly assembly)
{
    CultureInfo cultureInfo = new CultureInfo(culture);
    return GetTranslations(cultureInfo, baseName, assembly);
}


In your project you can call it now like  

Dictionary&lt;string,string&gt; translations = GetTranslations(culture, "Tesla.TSS.Globalization.SchedulingSystem", Assembly.GetExecutingAssembly());


and you could query this to get the translated strings.  

You could have a method ChangeLanguage(string culture) like so  

private string currentCultureName = "en-Us";
private void ChangeLanguage(string culture)
{
    if (string.IsNullOrWhiteSpace(culture)) 
    {
        currentCultureName = CultureInfo.CurrentUICulture.Name;
    }
    else
    {
        currentCultureName = culture;
    }

    translations = GetTranslations(culture, "Tesla.TSS.Globalization.SchedulingSystem", Assembly.GetExecutingAssembly());
}  


to change the current dictionary.  

Your former GetTranslatedString() method should then live as a class method (not static) inside your project and should look like so  

public string GetTranslatedString(string input, string culture)
{

    if (input == null) { throw new ArgumentNullException("input"); }

    if (string.IsNullOrWhiteSpace(input)) { return input; }

    if (currentCultureName != culture)
    {
        ChangeLanguage(culture);
    }

    string translation;
    if (translations.TryGetValue(input, out translation))
    {
        return translation;
    }

    return input;
} 


As you can see I have change quite a few things like 


adding braces {} although they are optional for single line if statements, but they will make the code less error prone.  
for a passed in input which is null this will throw an ArgumentNullException which is much better than just returning the null.  
for the passed in culture I didn't add a null check, because if the ChangeLanguage() method will be called with a null value the name of the CurrentUICulture will be used.