I like to generate a huge amount of text with a preprocessed T4 template. It would be ideal if the TransformText(); method writes to a steam instead of using
System.Text.StringBuilder GenerationEnvironment;
in the base class.
Does anybody know how to override this behaviour?
There doesn’t seem to be any intended extension point in the generated code that would allow you to do that. But if you look at the generated code, it looks something like this:
It’s clear that the call to
this.Write()is intended to call theWrite()method from the base class. But it doesn’t have to call that method, you can hide it in your non-generated part of the class:If you do this, the call to
TransformText()will actually write to theStreamWriter, which is exactly what you want.In reality, your code for the
Write()method might be more complicated, to mirror what the generatedWrite()does (mostly related to indenting the generated text). And the base class also contains other overloads ofWrite(), which you might need to hide as well.Alternatively, if you don’t want to mirror the code in the generated
Write(), you could callbase.Write()in yourWrite(), write the content of theStringWriterto your stream and then clear theStringWriter. But you would still need to deal with all the overloads ofWrite().