c# - 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?
解决方案
我会包装资源管理器:
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);
}
}
推荐阅读
- python - Python从文本中获取段落
- node.js - Joi 字段验证基于另一个字段的一组值
- mysql - Flutter 中的 MYSQL
- javascript - Flask - 加载外部 JavaScript 文件时出现 RefferenceError
- excel - 如何在VB中替换字符串?
- swift - 如何解开可选字符串,然后将其转换为 Double 数据类型?
- kotlin - mokk() 和 mokkClass() 有什么区别?
- sql - 如何通过在 SQL Server 中插入子查询来更新列
- c++ - 类型类的C ++变量声明不命名类型?
- javascript - 在嵌套树视图(或嵌套 JSON 对象)中搜索数据