I'm having a hard to trying to figure out if this is possible when I really don't even know what is called.
I have a APIKey (which can change with different builds) and we have another field that converts that APIKey to be more web friendly. It is the web friendly on that I would like to have the compiler calculate.
Currently we are recalculating the value every call to the webapi. I could easily just define a const and put in the webfriendly version of it, but I want to be a little fancier and offer less maintenance.
what I want is something like this.
public class WebSearch{
private const string ApiKey="jsdfjsd90234092j3_%sss";
private const string WebFriendlyKey=#Compiler(ApiKey.Replace("-","+"));
}
Where WebFriendlyKey is computed by the compiler.
I do not believe that the current C# compiler can do this. Maybe with the new modular compiler coming out (Roslyn).
What you can do right now is calculate the WebFriendlyKey just once each time your process starts, and store that for the lifetime of the process, which I assure you is just about as good as a constant from a performance perspective:
public class WebSearch{
private const string ApiKey="jsdfjsd90234092j3_%sss";
// This guy will only run once per AppDomain
private static readonly string WebFriendlyKey = ApiKey.Replace("-","+");
}