开发者

GetGlobalResourceObject or Resources.Resource - what's better?

开发者 https://www.devze.com 2022-12-13 14:50 出处:网络
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)

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?

  1. GetGlobalResourceObject("LocalizedText", "ErrorOccured")
  2. 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):

  1. You can specify a particular culture instead of using the CurrentCulture.
  2. 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.
  3. 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:

  1. You get compile-time errors if you access a resource that doesn't exist.
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号