0

Is it possible in .NET to make all strings within a class, namespace, file, etc. Verbatim String Literals.

There are always a file or two that simply need tons of escaping, where it would be nice to have all strings be considered Verbatim String Literals unless otherwise specified (IE opposite of default .NET behavior).

Use Case: In some of our constant declaration files (that don't belong in the Resource files). Also in helper files for Razor where we are escaping HTML over and over again.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
abc123
  • 17,855
  • 7
  • 52
  • 82
  • In Razor, a string will be literal, unless you use `Html.Raw(myString)` – Jacob Roberts May 18 '15 at 20:33
  • Why wouldn't they belong in resource files? – Joe Enos May 18 '15 at 20:35
  • @JacobRoberts not if you are calling a `.cs` helper method. – abc123 May 18 '15 at 20:36
  • 1
    You can use https://msdn.microsoft.com/en-us/library/w3te6wfz%28v=vs.110%29.aspx to do your encoding. `HttpServerUtility.HtmlEncode` – Jacob Roberts May 18 '15 at 20:36
  • @JoeEnos because a lot of them are HTML strings (I'm new to the project). – abc123 May 18 '15 at 20:36
  • @JacobRoberts currently we are using `MvcHtmlString.Create` but the string passed still needs to be escaped with `"` and any other escaped char. – abc123 May 18 '15 at 20:40
  • An attribute isn't going to work. I don't think overriding the `OnActionExecuting` in your controller will work either. Seems you need a decorator and wrap all of your strings with that so you can handle the special formatting. – Jacob Roberts May 18 '15 at 20:46

2 Answers2

0

Simply read a text file at run-time.

A compile time .NET file really isn't available to the best of my knowledge.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
0

There's no built in compiler switch to do this. You could probably hack something up with Roslyn depending on which version of VS you're using, but it'd be really confusing for other users I suspect.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • 2
    IMO Even if you could do it, it wouldn't just be confusing, it would no longer be C# at that point - the language has rules, so if you are not following those rules, I wouldn't consider it the same language. – Joe Enos May 18 '15 at 20:35
  • I feel if you implemented it as a flag at the top of the file it wouldn't be *AS* confusing. – abc123 May 18 '15 at 20:37