I have a bunch of strings that are dependent on static dictionaries and each other, but are never modified by user code. Something like as follows:
public static RegexDicts
{
public static readonly Dictionary<string, string> dict = new Dictionary<string, string>{
{"One", "1"},
//And so on
}
}
public static class RegexStrings
{
public static readonly string String1 = String.Join("|", RegexDicts.dict.Keys);
public static readonly string String2 = "Hi! (" + String1 + "(";
//...
}
Right now, all of these methods have to be executed every time the program executes (even if only once.)
What’s the best way to shift these method executions to the compiler, leaving the actual executable with the intended constants? Even better yet, is it possible to extend this to that dictionary itself – to build the dictionary by reading a file at compile-time?
I’m amicable to doing this with code generation, if I can generate the code and compile it simultaneously.
You could do this via a T4 template. For details, see Code Generation and T4 Templates.