本文介绍了哪个最佳做法是读取资源文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取资源文件的最佳实践.

我正在尝试以下两种方法,但每种方法似乎都有其自身的缺点.

方法1:使用CreateFileBasedResourceManger函数.但是由于下面的代码.句柄数正在增加.

Which best practice to read resource file.

I am trying below two approaches but each has its own drawback it seems.

Approach 1: Using CreateFileBasedResourceManger function. But due to below code..Handle count is getting increased.

ResourceManager resourceReader = ResourceManager.CreateFileBasedResourceManager("MyResources", appFilePath, null);
               string translatedMessage = resourceReader.GetString(keyName);
               if (!string.IsNullOrEmpty(translatedMessage))
               {
                   return translatedMessage;

               }
               else
               {
                   englishKey = keyName.Substring(keyName.Length - 4, 4);
                   keyName = keyName.Replace(englishKey, "_ENG");
                   translatedMessage = resourceReader.GetString(keyName);
                   if (!string.IsNullOrEmpty(translatedMessage))
                   {
                       return translatedMessage;
                   }
                   throw new FormatterException("Key is not present in the resoucre file");
               }



方法2:使用ResourceSet构造函数.句柄数减少,但性能下降.



Approach 2 : Using ResourceSet constructor..Handle count is getting reduced but performance has been down.

using (ResourceSet resx = new ResourceSet(appFilePath + "\\MyResources.resources"))
            {
                    string translatedMessage=  resx.GetString(keyName);

                if (!string.IsNullOrEmpty(translatedMessage))
                {
                    return translatedMessage;

                }
                else
                {
                    englishKey = keyName.Substring(keyName.Length - 4, 4);
                    keyName = keyName.Replace(englishKey, "_ENG");
                    translatedMessage = resx.GetString(keyName);
                    if (!string.IsNullOrEmpty(translatedMessage))
                    {
                        return translatedMessage;
                    }
                    throw new FormatterException("Key is not present in the resoucre file");
                }
            }


请建议还有其他方法可以通过保持性能和处理计数来读取资源文件.


Please suggest is there any other way to read resource file by keeping performance up and handle count.

推荐答案


这篇关于哪个最佳做法是读取资源文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 01:47