Versuch es damit. Eine Erweiterungsmethode für einen String. Zu verwenden:
string s = "namespace A{class B{}}".FormatAsSourceCode();
public static class Extensions { public static string FormatAsSourceCode(this string s) { int indent = 0; bool LineBreak = false; StringBuilder sb = new StringBuilder();