GetGlobalResourceObject or Resources.Resource - what's better?
I have an application that is multilingual. I'm using the out-of-the-box .Net features for this. Each language has its own file in the App_GlobalResources (see iamge below)
In the code behind what is better?
- GetGlobalResourceObject("LocalizedText", "ErrorOccured")
- Resources.LocalizedText.ErrorOccured
The 2nd one uses less code and it's type safe, it will return an error during开发者_运维技巧 compile time and not run time.
alt text http://img340.imageshack.us/img340/5562/langl.gif
These are the advantages of each approach:
Advantages of GetGlobalResourceObject
(and GetLocalResourceObject
):
- You can specify a particular culture instead of using the CurrentCulture.
- You can use a late-bound expression (i.e. a string) to decide which resource to load. This is useful if you can't know ahead of time which resource you will need to load.
- It works with any resource provider type. For example, it works not only with the built-in default RESX-based provider but it'll work the same against a database-based provider.
Advantages of strongly-typed RESX types:
- You get compile-time errors if you access a resource that doesn't exist.
- You get Intellisense while working on the project.
So, as with many "which is best" questions, the answer is: It depends! Choose the one that has advantages that will benefit your particular scenarios the most.
So use the second one, if you know up-front what the resource file and key will be.
The GetGlobalResourceObject()
method is useful if you don't know what the resource file or (more likely) the key will be at compile time.
精彩评论