如上一个问题所述,我有一组字符串资源,它们在程序的生命周期内不会改变,但只能通过对字符串列表的昂贵操作来获得:
public static class Patterns
{
Public static readonly string StateUS =
@"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.StateUS) + @")\b";
Public static readonly string Countries =
@"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.Countries) + @")\b";
//.....
}
为了提高性能,我使用 T4 生成了所需的字符串,它从文件中读取列表并输出资源文件Regex.restext
:
StatesUS=A(LA(SK|BAM)A|RKANSAS)|B.....
Countries=UNITED (STATES|KINGDOM)|CA(NAD|MBODI)A.....
然后将此文件编译复制到构建后的目标目录:
cd $(ProjectDir)
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\resgen.exe" /compile Regex.restext
copy /Y Regex.resources "$(TargetDir)MyProject.Resources.Lists.resources"
但是当我尝试在静态构造函数中加载这些值时Patterns
:
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("MyProject.Resources.Lists", @".\", null);
StateUS = rm.GetString("StateUS")
我收到以下错误:
InnerException: System.Resources.MissingManifestResourceException
Message=Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.
System.Resources.FileBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
但这是一个文化中立的项目——资源仅用于存储。那么为什么我需要指定一种文化呢?我将如何/在哪里这样做?
如果我在加载资源时采用了错误的方法,那么更好的方法是什么?