public static string DecompileCSharp(string assemblyFileName, DecompilerSettings settings = null) { using (var module = ModuleDefinition.ReadModule(assemblyFileName)) { var typeSystem = new DecompilerTypeSystem(module); CSharpDecompiler decompiler = new CSharpDecompiler(typeSystem, settings ?? new DecompilerSettings()); decompiler.AstTransforms.Insert(0, new RemoveEmbeddedAtttributes()); decompiler.AstTransforms.Insert(0, new RemoveCompilerAttribute()); decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers()); var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile(); StringWriter output = new StringWriter(); var visitor = new CSharpOutputVisitor(output, FormattingOptionsFactory.CreateSharpDevelop()); syntaxTree.AcceptVisitor(visitor); string fileName = Path.GetTempFileName(); File.WriteAllText(fileName, output.ToString()); return(fileName); } }
public void TestBug37383() { var fmt = FormattingOptionsFactory.CreateMono(); var indent = Helper.CreateEngine(@" using System; class Test { public void TestCase(ConsoleKey k) { switch (k) { case ConsoleKey.F1: $ break; } } }", fmt); Assert.AreEqual("\t\t\t", indent.ThisLineIndent); }
public void TestStateAfterDoublePreprocessorIf() { var policy = FormattingOptionsFactory.CreateMono(); policy.AlignToFirstMethodCallArgument = policy.AlignToFirstIndexerArgument = false; var indent = Helper.CreateEngine(@" class Foo { void Test () { #if true #if true if (true) return;$ ", policy); Assert.AreEqual("\t\t\t", indent.ThisLineIndent); Assert.AreEqual("\t\t", indent.NextLineIndent); }
public void TestIndentPreprocessorStatementsAdd() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.IndentPreprocessorDirectives = true; Test(policy, @"class Test { #region DEBUG #endregion }", @"class Test { #region DEBUG #endregion }"); }
public void TestMemberReferenceFormatting() { var policy = FormattingOptionsFactory.CreateMono(); Test(policy, @" class Test { void Init () { System. Console .WriteLine (); } }", @" class Test { void Init () { System.Console.WriteLine (); } }"); }
public void TestPropertyIndentationCase2() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); Test(policy, @"class Test { public int Prop { get; set; } }", @"class Test { public int Prop { get; set; } }"); }
public void TestFixedStatementIndentation() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.ClassBraceStyle = BraceStyle.EndOfLine; Test(policy, @"class Test { Test TestMethod () { fixed (object* obj = &obj) ; } }", @"class Test { Test TestMethod () { fixed (object* obj = &obj) ; } }"); }
public void TestBreakSemicolon() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); Test(policy, @"class Test { Test TestMethod () { break ; } }", @"class Test { Test TestMethod () { break; } }"); }
public void TestForIndentation() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.ClassBraceStyle = BraceStyle.EndOfLine; Test(policy, @"class Test { Test TestMethod () { for (;;) { } } }", @"class Test { Test TestMethod () { for (;;) { } } }"); }
public void TestMethodDeclarationAlignment() { var policy = FormattingOptionsFactory.CreateMono(); policy.AlignToFirstMethodDeclarationParameter = true; Test(policy, @"class Test { void TestMe (int test, string fooo) { } }", @"class Test { void TestMe (int test, string fooo) { } }"); }
public void TestComplexIfElseElsePlacement_AlignmentOn() { var policy = FormattingOptionsFactory.CreateMono(); // policy.AlignElseInIfStatements = true; var indent = Helper.CreateEngine(@" class Foo { void Test () { if (1 > 0) a = 1; else if (2 < 10) a = 2; else$ ", policy); Assert.AreEqual("\t\t", indent.ThisLineIndent); Assert.AreEqual("\t\t\t", indent.NextLineIndent); }
public void TestDoWhileIndentation() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.ClassBraceStyle = BraceStyle.EndOfLine; Test(policy, @"class Test { Test TestMethod () { do { } while (true); } }", @"class Test { Test TestMethod () { do { } while (true); } }"); }
public void TestConstructorInitializer() { var policy = FormattingOptionsFactory.CreateMono(); Test(policy, @" class Foo { public Foo (): base (0) { } } ", @" class Foo { public Foo () : base (0) { } } "); }
public void TestUsingIndentation() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.ClassBraceStyle = BraceStyle.EndOfLine; Test(policy, @"class Test { Test TestMethod () { using (var o = new MyObj()) { } } }", @"class Test { Test TestMethod () { using (var o = new MyObj()) { } } }"); }
public void TestBrackets_StackedIfElse_BreakNestedStatementsOnAnonymousBlock() { CSharpFormattingOptions fmt = FormattingOptionsFactory.CreateMono(); fmt.AlignEmbeddedStatements = false; var indent = Helper.CreateEngine(@" class Foo { void Test () { if (true) lock (this) if (true) if (false) ; { } // this should break the nested statements else $ " , fmt); Assert.AreEqual("\t\t", indent.ThisLineIndent); Assert.AreEqual("\t\t\t", indent.NextLineIndent); }
void GenerateCode(AstBuilder astBuilder, ITextOutput output) { var syntaxTree = astBuilder.SyntaxTree; syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); // generate AST var transform = new CSharpToCpp(); transform.Run(syntaxTree); var include = new IncludeVisitor(); syntaxTree.AcceptVisitor(include); // generate include string include_name = include.typename + ".h"; output.WriteLine("#include <QuantKit/Event/" + include_name + ">"); output.WriteLine("#include <QuantKit/EventType.h>"); output.WriteLine("#include \"../Event_p.h\""); output.WriteLine("#include \"DataObject_p.h\""); output.WriteLine("#include \"Tick_p.h\""); output.WriteLine(); //Generate cpp Code var outputFormatter = new TextOutputFormatter(output) { FoldBraces = true }; var formattingPolicy = FormattingOptionsFactory.CreateAllman(); syntaxTree.AcceptVisitor(new PrivateHppOutputVisitor(outputFormatter, formattingPolicy)); syntaxTree.AcceptVisitor(new PrivateCppOutputVisitor(outputFormatter, formattingPolicy)); syntaxTree.AcceptVisitor(new CppOutputVisitor(outputFormatter, formattingPolicy)); // generate endif output.WriteLine(); }
public void TestBlankLinesInsideRegion() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.BlankLinesInsideRegion = 2; var adapter = Test(policy, @"class Test { #region FooBar void AMethod () { } #endregion }", @"class Test { #region FooBar void AMethod () { } #endregion }", FormattingMode.Intrusive); policy.BlankLinesInsideRegion = 0; Continue(policy, adapter, @"class Test { #region FooBar void AMethod () { } #endregion }", FormattingMode.Intrusive); }
public void TestDoWhileWithoutBrackets() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); Test(policy, @"class Test { Test TestMethod () { do FooBar(); while (true); } }", @"class Test { Test TestMethod () { do FooBar (); while (true); } }"); }
public void TestBlankLinesBeforeFirstDeclaration() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.MinimumBlankLinesBeforeFirstDeclaration = 2; var adapter = Test(policy, @"namespace Test { class Test { } }", @"namespace Test { class Test { } }", FormattingMode.Intrusive); }
public void TestBlankLinesBetweenEventFields() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.MinimumBlankLinesBetweenEventFields = 1; var adapter = Test(policy, @"class Test { public event EventHandler a; public event EventHandler b; public event EventHandler c; }", @"class Test { public event EventHandler a; public event EventHandler b; public event EventHandler c; }", FormattingMode.Intrusive); }
public void TestBlankLinesBetweenFields() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.MinimumBlankLinesBetweenFields = 1; var adapter = Test(policy, @"class Test { int a; int b; int c; }", @"class Test { int a; int b; int c; }", FormattingMode.Intrusive); }
static string GetPartialMethodSignature(MethodDeclaration declaration) { if (declaration.Parameters.Count > 0) { using (var writer = new StringWriter()) { var visitor = new CSharpOutputVisitor(writer, FormattingOptionsFactory.CreateAllman()); var parameterIndex = 0; foreach (var parameter in declaration.Parameters) { if (parameterIndex > 0) { writer.Write(","); } parameter.AcceptVisitor(visitor); parameterIndex++; } return(string.Format("{0}({1})", GetNameWithTypeParameters(declaration), writer)); } } return(string.Format("{0}()", GetNameWithTypeParameters(declaration))); }
public void TestIndentNamespaceBody() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.ClassBraceStyle = BraceStyle.DoNotChange; policy.NamespaceBraceStyle = BraceStyle.EndOfLine; policy.IndentNamespaceBody = true; var adapter = Test(policy, @" namespace Test { class FooBar {} }" , @"namespace Test { class FooBar {} }"); policy.IndentNamespaceBody = false; Continue(policy, adapter, @"namespace Test { class FooBar {} }"); }
public void TestForeachBracketPlacement() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.StatementBraceStyle = BraceStyle.EndOfLine; Test(policy, @"class Test { Test TestMethod () { foreach (var obj in col) {} } }", @"class Test { Test TestMethod () { foreach (var obj in col) { } } }"); }
public void TestKeepCommentsAfterStatement() { var policy = FormattingOptionsFactory.CreateMono(); policy.KeepCommentsAtFirstColumn = true; Test(policy, @"class Test { void TestMe () { FooBar (); // comment } }", @"class Test { void TestMe () { FooBar (); // comment } }"); }
public void TestUsingInsideNamespace() { CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.UsingPlacement = UsingPlacement.InsideNamespace; Test(policy, @"namespace TestNamespace { using System; class Test { } }", @"namespace TestNamespace { using System; class Test { } }"); }
public void TestKeepCommentsAtFirstColumnFalse() { var policy = FormattingOptionsFactory.CreateMono(); policy.KeepCommentsAtFirstColumn = false; Test(policy, @"class Test { void TestMe () { // comment } }", @"class Test { void TestMe () { // comment } }"); }
public void TestForInitializerIteratorConditionFormatting() { var policy = FormattingOptionsFactory.CreateMono (); Test (policy, @" class Test { void Init () { for (int i = 12; i < 10; i ++) { } } }", @" class Test { void Init () { for (int i = 12; i < 10; i++) { } } }"); }
public void TestBrackets_StackedIfElse_AlignElseToCorrectIf2() { CSharpFormattingOptions fmt = FormattingOptionsFactory.CreateMono(); fmt.AlignEmbeddedIfStatements = false; var indent = Helper.CreateEngine(@" class Foo { void Test () { if (true) lock (this) if (true) if (false) { } else ; else $ " , fmt); Assert.AreEqual("\t\t\t\t", indent.ThisLineIndent); Assert.AreEqual("\t\t\t\t\t", indent.NextLineIndent); }
public void TestUsingExpressionFormatting() { var policy = FormattingOptionsFactory.CreateMono (); Test (policy, @" class Test { void Init () { using (var o = new object( )) { } } }", @" class Test { void Init () { using (var o = new object ()) { } } }"); }