Want to create either a stream or a file

S

Steve

I am reworking a class that generates source code. I want to add a
"preview" function, much like the refactor dialog in vs2005. To facilitate
this, I have changed what used to be my "writeToFile" method to "generate" -
the plan is, I will generate the text in memory, then I can either show it
in a control or pass it to the method that does the file IO stuff.

I'm trying to determine the best class' to use for this, I was hoping there
was something that would let me build the string, then pass it to a writer
class, but I haven't found it yet. Currently I'm using StringWriter to
generate the source, then when I want to write the code, I use
StreamWriter.Write() passing it my StringWriter.ToString();

This seems ugly to me. Is there another way that would be cleaner? I was
hoping for something like setting a StreamWriter's internal data to my
StringWriter, or casting a StringWriter to a StreamWriter... something like
that. They seem so closely related.

Any ideas?

Thanks for reading,
Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

I would have your generate method take a TextWriter. That way, if you
want it in a string that you can get later, you can pass a StringWriter. If
you want to write it to a file, then you can use a StreamWriter instance
with a FileStream backing it.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top