From be8d26b248c12e548b15a895ae6c1382d2232e4f Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Wed, 15 Feb 2017 22:43:59 +0000 Subject: [PATCH] Extract C# using generation into its own method. --- .../Generators/CSharp/CSharpSources.cs | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index b41d9ce6..281e671c 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -139,20 +139,41 @@ namespace CppSharp.Generators.CSharp #endregion + public Module Module => TranslationUnits.Count == 0 ? + Context.Options.SystemModule : TranslationUnit.Module; + public override void Process() { GenerateFilePreamble(); - var module = TranslationUnits.Count == 0 ? - Context.Options.SystemModule : TranslationUnit.Module; + GenerateUsings(); + + if (!string.IsNullOrEmpty(Module.OutputNamespace)) + { + PushBlock(CSharpBlockKind.Namespace); + WriteLine("namespace {0}", Module.OutputNamespace); + WriteStartBraceIndent(); + } + foreach (var unit in TranslationUnits) + unit.Visit(this); + + if (!string.IsNullOrEmpty(Module.OutputNamespace)) + { + WriteCloseBraceIndent(); + PopBlock(NewLineKind.BeforeNextBlock); + } + } + + public void GenerateUsings() + { PushBlock(CSharpBlockKind.Usings); WriteLine("using System;"); WriteLine("using System.Runtime.InteropServices;"); WriteLine("using System.Security;"); var internalsVisibleTo = (from m in Options.Modules - where m.Dependencies.Contains(module) + where m.Dependencies.Contains(Module) select m.LibraryName).ToList(); if (internalsVisibleTo.Any()) WriteLine("using System.Runtime.CompilerServices;"); @@ -166,22 +187,6 @@ namespace CppSharp.Generators.CSharp if (internalsVisibleTo.Any()) NewLine(); - - if (!string.IsNullOrEmpty(module.OutputNamespace)) - { - PushBlock(CSharpBlockKind.Namespace); - WriteLine("namespace {0}", module.OutputNamespace); - WriteStartBraceIndent(); - } - - foreach (var unit in TranslationUnits) - unit.Visit(this); - - if (!string.IsNullOrEmpty(module.OutputNamespace)) - { - WriteCloseBraceIndent(); - PopBlock(NewLineKind.BeforeNextBlock); - } } public override bool VisitNamespace(Namespace @namespace)