GetGlobalResourceObject 或 Resources.Resource - 哪个更好?
我有一个多语言的应用程序。为此,我使用了开箱即用的 .Net 功能。每种语言在 App_GlobalResources 中都有自己的文件(见下图)
在后面的代码中什么更好?
- GetGlobalResourceObject("LocalizedText", "ErrorOccured")
- Resources.LocalizedText.ErrorOccured
第二个使用较少的代码并且类型安全,它将在编译时而不是运行时返回错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是每种方法的优点:
GetGlobalResourceObject
(和GetLocalResourceObject
)的优点:强类型 RESX 类型的优点:
因此,与许多“哪个最好”的问题一样,答案是:这取决于情况!选择最有利于您的特定场景的优势。
These are the advantages of each approach:
Advantages of
GetGlobalResourceObject
(andGetLocalResourceObject
):Advantages of strongly-typed RESX types:
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.
因此,如果您预先知道资源文件和密钥是什么,请使用第二个。
如果您不知道编译时资源文件或(更有可能)密钥是什么,则
GetGlobalResourceObject()
方法非常有用。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.