I have a C# regex-parser program with three files in it, each containing a static class:
1) one static class filled with string dictionaries
static class MyStringDicts
{
internal static readonly Dictionary<string, string> USstates =
new Dictionary<string, string>()
{
{ "ALABAMA", "AL" },
{ "ALASKA", "AK" },
{ "AMERICAN SAMOA", "AS" },
{ "ARIZONA", "AZ" },
{ "ARKANSAS", "AR" }
// and so on
}
// and some other dictionaries
}
2) A class that compiles these values into Regex
public static class Patterns
{
Public static readonly string StateUS =
@"\b(?<STATE>" + CharTree.GenerateRegex(Enumerable.Union(
AddrVals.USstates.Keys,
AddrVals.USstates.Values))
+ @")\b";
//and some more like these
}
3) some code that runs regular expressions based on these strings:
public static class Parser
{
// heavily simplified example
public static GroupCollection SearchStringForStates(string str)
{
return Regex.Match(str,
"^" + Patterns.StateUS,
RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase).Groups;
}
}
I'd like to be able to generate 2) as with a T4 template, as all of this concatenation is identical on every execution:
@"\b(?<STATE><#=CharTree.GenerateRegex(Enumerable.Union(
AddrVals.USstates.Keys,
AddrVals.USstates.Values)#>)\b";
This works, but if I create a new member of MyStringDicts
, or add/remove some values from its dictionaries, the T4 template won't recognize them until exclude Patterns.cs from compilation and recompile. As Parser
depends on Patterns
, this really isn't an option - I need the T4 transformation to take into account changes to other files in the same build.
Things I don't want do do:
- Split
MyStringDicts
into its own project. I'd like to keep the files in one project, as they are a logical unit. - Just move
MyStringDicts
into the top of Patterns.cs. I need the MyStringDicts members for other purposes, too (for dictionary lookups, or in other T4 templates, for example.)
I adopted the advice here about using T4Toolbox's VolatileAssembly
and such, but that seems to only work for the reverse direction, when the class files need to be recompiled after editing the T4 template.
Is what I want possible?
edited for clarity