首页 > 解决方案 > How to create separate resource files for debugging and releasing?

问题描述

Is it possible to create two files, for example Text.Debug.resx and Text.Release.resx, where the appropriate resource file is automatically loaded during debugging and releasing of the program?

标签: c#asp.net-mvcembedded-resource

解决方案


我会包装资源管理器:

public class Resources
{
    private readonly ResourceManager _resourceManager;

    public Resources()
    {
#if DEBUG
        const string configuration = "Debug";
#else
        const string configuration = "Release";
#endif

        _resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);
    }

    public string GetString(string resourceKey)
    {
        return _resourceManager.GetString(resourceKey);
    }
}

显然,在更新管理器时适当地修改命名空间。

编辑

您还可以将其实现为静态类,以避免必须新建包装器的实例:

public static class Resources
{
    private static ResourceManager _resourceManager;

    public static string GetString(string resourceKey)
    {
        if (_resourceManager != null)
        {
            return _resourceManager.GetString(resourceKey);
        }

#if DEBUG
        const string configuration = "Debug";
#else
        const string configuration = "Release";
#endif

        _resourceManager = new ResourceManager($"StackOverflow.Text.{configuration}", typeof(Resources).Assembly);

        return _resourceManager.GetString(resourceKey);
    }
}

推荐阅读