Created
September 28, 2015 08:47
-
-
Save xtrmstep/ad6c973168d063301758 to your computer and use it in GitHub Desktop.
Write an object using StringWriter to StringBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
namespace ConsoleApplication2 | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
TheObject obj = new TheObject | |
{ | |
IntField = 999 | |
}; | |
StringBuilder sp = new StringBuilder(); | |
using (TextWriter tw = new StringWriter(sp)) | |
{ | |
obj.PrintTo(tw); | |
} | |
Console.Write(sp.ToString()); | |
Console.ReadKey(); | |
} | |
} | |
internal class TheObject | |
{ | |
public int IntField | |
{ | |
get; | |
set; | |
} | |
public void PrintTo(TextWriter writer) | |
{ | |
writer.WriteLine("TheObject : {"); | |
writer.WriteLine(" IntField = {0}", IntField); | |
writer.WriteLine("}"); | |
writer.WriteLine(""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment