问题描述
是否有可能将一个大的Resource..x文件物理上拆分为多个文件,并且仍然使用相同的syntrix来获取数据
Is it possible to split one big Resource(.resx) file into several files physically and still use the same syntex to get data
即
GlobalResource.resx
GlobalResource.resx
进入
GlobalResource1.resx,GlobalResource2.resx,GlobalResource3.resx,...
GlobalResource1.resx, GlobalResource2.resx, GlobalResource3.resx, ...
,所有文件都将被合并为一个文件,并像
and all file will be compliled into one and use like
Resources.GlobalResource.errParamNameReq
在Asp.net 3.5中使用它.在处理svn时遇到问题,每次尝试检入时都遇到冲突,因此我想在编译器将其视为一个文件的同时将其拆分为多个文件.
using it in Asp.net 3.5. having problem while working on svn, every time it get conflicted while try to checkin, so i thought to split it into many files while the compiler treat it as one.
推荐答案
每个资源文件都有一个代码隐藏文件,该文件用于将资源项声明为静态属性.此外,Visual Studio还使用了许多自动代码生成工具来在xml/资源编辑器数据和文件背后的代码之间进行同步.可以将资源拆分为多个文件,但是稍后自动生成代码的效果可能会比单纯合并更改引起更多的问题.一种可能的解决方案是创建不同的资源文件.然后有一个类,为每个资源管理器保留 ResourceManager
实例的集合.像这样:
Every resource file has a code-behind file which is used to declare the resource items as static properties. Also a lot of automated code generation is used by tools as Visual Studio to synchronize between the xml/resource editor data and the code behind file. Splitting a resource into more than one file might be possible, but the effects of automated code generation later can cause even more problems than simply merging your changes. A possible solution is to create different resource files. Then have a class that keeps a collection of ResourceManager
instance for every resource manager. Something like this:
public class ResourceWrapper
{
private ICollection<ResourceManager> resourceManagers;
public ResourceWrapper(IEnumerable<ResourceManager> managers)
{
resourceManagers = new List<ResourceManager>(managers);
}
public string GetString(string key)
{
foreach (var resourceManager in resourceManagers)
{
string res = resourceManager.GetString(key);
if (res != null)
{
return res;
}
}
return null;
}
public string GetString(string key, CultureInfo culture)
{
foreach (var resourceManager in resourceManagers)
{
string res = resourceManager.GetString(key, culture);
if (res != null)
{
return res;
}
}
return null;
}
}
您只需要获取所有资源并将它们传递给构造函数即可.您可能希望在创建包装器实例后保留它,以避免为每个资源文件创建一个资源管理器-静态文件场会这样做.上面的代码唯一的问题是不能保证2个或更多资源文件定义相同的密钥.如果发生这种情况,代码将仅返回第一个匹配项.
You only have to get all resources and pass them to the constructor. You might want to keep the wrapper instance once it is created to avoid creating a resource manager for each resource file - a static filed will do. The only issue with the above code is that there is no guarantee that 2 or more resource files define the same key. If this happens the code will return the first match only.
这篇关于将大型资源文件物理上拆分为多个文件,而编译器将其视为一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!