我知道從資源文件中獲取字符串的兩種方法。直接訪問resource.resx而不是ResourceManager會更好嗎?
方法1:直接訪問。
string str = _NAMESPACE.Properties.Resources.HelloWorld;
方法2:通過ResourceManager。
ResourceManager rm = new ResourceManager("_NAMESPACE.Properties.Resources", Assembly.GetExecutingAssembly());
string str = rm.GetString("HelloWorld");
如果有多個本地化資源文件,這兩個方法都會得到正確的本地化字符串。
如果我在字符串鍵中輸入錯誤,method1會在構建時失敗。方法2將在運行時間之前失敗。出於這個原因,我認爲method1比method2更好。
我在這個論壇發現了一些類似的問題。但是,對於我的問題,我仍然無法得到很好的答案。
Why is it better to call the ResourceManager class as opposed to loading resources directly by name?
是真的方法1更好?
看看第二個代碼中的所有硬編碼字符串!第一個更容易重構。 –
如果您希望將資源劃分爲多個*資源文件(例如,「Properties.Messages」,「Properties.Icons」等),請參閱[此問題](http://stackoverflow.com/q/5246631/1497596)及其答案。 – DavidRR