使用 *.resx 文件存储字符串值对

发布于 2024-09-27 11:09:31 字数 205 浏览 0 评论 0原文

我有一个需要字符串值之间映射的应用程序,因此本质上是一个可以保存键值对的容器。我没有使用字典或名称-值集合,而是使用了在代码中以编程方式访问的资源文件。据我所知,资源文件用于多语言实现等本地化场景。不过,我喜欢它们的强类型特性,这可以确保如果值发生更改,应用程序将无法编译。

但是我想知道使用 *.resx 文件进行简单的键值对存储而不是使用更传统的编程类型是否有任何重要的缺点。

I have an application that requires mappings between string values, so essentially a container that can hold key values pairs. Instead of using a dictionary or a name-value collection I used a resource file that I access programmatically in my code. I understand resource files are used in localization scenarios for multi-language implementations and the likes. However I like their strongly typed nature which ensures that if the value is changed the application does not compile.

However I would like to know if there are any important cons of using a *.resx file for simple key-value pair storage instead of using a more traditional programmatic type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-10-04 11:09:31

我突然想到了两个缺点:

  • 它需要 I/O 操作来读取键/值对,这可能会导致性能显着下降,
  • 如果让标准.Net 逻辑来解决加载资源,它总是会尝试找到CultureInfo.CurrentUICulture属性对应的文件;如果您决定实际上想要拥有多个 resx-es(即每种语言一个),这可能会出现问题;这可能会导致性能进一步下降。

顺便提一句。难道您不能只创建包含属性的帮助器类或结构吗,如下所示:

public static class GlobalConstants
{
    private const int _SomeInt = 42;
    private const string _SomeString = "Ultimate answer";

    public static int SomeInt
    {
        get
        {
            return _SomeInt;
        }
    }

    public static string SomeString
    {
        get
        {
            return _SomeString;
        }
    }
}

然后您可以以与资源文件完全相同的方式访问这些属性(我假设您已经习惯了这种样式):

textBox1.Text = GlobalConstants.SomeString;
textBox1.Top = GlobalConstants.SomeInt;

也许它不是最好的要做的事情,但我坚信这仍然比使用资源文件更好......

There are two cons which I can think of out of the blue:

  • it requires I/O operation to read key/value pair, which may result in significant performance decrease,
  • if you let standard .Net logic to resolve loading resources, it will always try to find the file corresponding to CultureInfo.CurrentUICulture property; this could be problematic if you decide that you actually want to have multiple resx-es (i.e. one per language); this could result in even further performance degradation.

BTW. Couldn't you just create helper class or structure containing properties, like that:

public static class GlobalConstants
{
    private const int _SomeInt = 42;
    private const string _SomeString = "Ultimate answer";

    public static int SomeInt
    {
        get
        {
            return _SomeInt;
        }
    }

    public static string SomeString
    {
        get
        {
            return _SomeString;
        }
    }
}

You can then access these properties exactly the same way, as resource files (I am assuming that you're used to this style):

textBox1.Text = GlobalConstants.SomeString;
textBox1.Top = GlobalConstants.SomeInt;

Maybe it is not the best thing to do, but I firmly believe this is still better than using resource file for that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文