How does reflection work on resource repositories

I want to understand how such a code works to retrieve the resource of the specified ThreadUI Culture?

var value = resourceAssembly
      .GetType("Namespace.FooBar")
      .GetProperty("Hello")
      .GetValue(null, null) as string;

If my ThreadUI Culture is english, I get the english value. If it is german, I get the german value. Ok, fine. But how does it work inside?

Jon Skeet
people
quotationmark

If you open up the generated C# file corresponding to that type, you'll see something like this:

internal static string Hello {
    get {
        return ResourceManager.GetString("Hello", resourceCulture);
    }
}

Unless you specific resourceCulture (by setting the Culture property) it will still be null, so the above will be equivalent to:

return ResourceManager.GetString("Hello", null);

The ResourceManager property returns a System.Resources.ResourceManager, and if you look at the docs for ResourceManager.GetString(string, CultureInfo) you'll see:

In desktop apps, if culture is null, the GetString(String, CultureInfo) method uses the current UI culture obtained from the CultureInfo.CurrentUICulture property.

So that's all it is - just a call to a library method which uses the current UI culture by default.

people

See more on this question at Stackoverflow