Search code examples
c#localizationglobalization

"GetResourceSet" method of "ResourceManager" [System.Resources] class returns empty - even if it exists in collection


I am using following code to retrieve the Resource file values in C#

        CultureInfo cultureInfo = new System.Globalization.CultureInfo("zh-CN"); // just hard coded to make question simpler
        ResourceManager rm = new ResourceManager("SchedulingSystem", Assembly.GetExecutingAssembly());

        var entry = rm.GetResourceSet(cultureInfo, true, true)
         .OfType<DictionaryEntry>()
         .FirstOrDefault(e => e.Value.ToString() == input);//input => "No_Records"

        var key = entry.Key.ToString();
        return key;

Both Key and Value returns as null, but if I use "Quick View" in Visual studio, I can see the values as shown in the Image, The input "No_Records" can be seen in Quick view of object.

I have also referred stack overflow quesitons like [Why does ResourceManager.GetResourceSet return null on the first request after a build? (C#) enter image description here

What I might have missed in my c# code?


Solution

  • I am not sure if the ResourceSet dictionary keys are case or culture sensitive, but there are two possible issues with your implementation:

    1. You are using Value not Key

    2. You are performing a straight comparison on the values instead of mitigating culture and case-sensitive differences.

    Changing to the following should resolve your issues:

    .FirstOrDefault(e => e.Key.ToString().Equals(input, StringComparison.InvariantCultureIgnoreCase));