Why is it better to call the ResourceManager class as opposed to loading resources directly by name?

北城以北 提交于 2019-12-03 14:55:06

Well, there's no reason to use the ResourceManager directly (some exceptions to that will apply), because if you use generated code from the resx-Files all it does is the following:

public static string MyResourceName {
    get {
        return ResourceManager.GetString("MyResourceName", resourceCulture);
    }
}

This is great, since you get Compile-Time validation of your resource-names for free!

http://www.c-sharpcorner.com/uploadfile/prvn_131971/chapter-i-resources-and-localization/

Internally, the generated class uses an instance of the ResourceManager class, which is defined in the System.Resources namespace. The instance of this class is accessible through the generated class's ResourceManager property. Internally, the property procedures for accessing the embedded resources themselves are just wrappers around calls of one of the GetXxx methods (that is, GetString or GetStream) of this ResourceManager instance. For example, the resource that is accessible through the generated property procedure Resources.MyResourceStrings.

So calling resources directly by name you're using YourResources.ResourceManager.GetString() method anyway.

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