title: Roslyn : How to get the Namespace of a DeclarationSyntax with Roslyn C#
body: <p>I have a c# solution that contains some class files. With Roslyn I am able to parse a solution to obtain a list of projects within the solution. From there, I can get the documents in each project. Then, I can get a list of every ClassDeclarationSyntax. <em>This is the starting point.</em></p>
  
  <pre><code>        foreach (var v in _solution.Projects)
          {
              //Console.WriteLine(v.Name.ToString());
              foreach (var document in v.Documents)
              {
                  SemanticModel model = document.GetSemanticModelAsync().Result;
                  var classes = document.GetSyntaxRootAsync().Result.DescendantNodes().OfType&lt;ClassDeclarationSyntax&gt;();
                  foreach(var cl in classes)
                  {
  // Starting around this point...
                      ClassDiagramClass cls = new ClassDiagramClass(cl, model);
                      diagramClasses.Add(cls);
                  }
              }
          }
  </code></pre>
  
  <p>From these objects I want to be able to get the Namespace of the variables used within each class. See file 1 has a method "getBar()" that returns an object of type B.Bar. The namespace is important because it tells you which type of Bar is really being returned. </p>
  
  <p><strong>File1.cs</strong></p>
  
  <pre><code>using B;
  namespace A {
      public class foo(){
          public Bar getBar(){ return new Bar();}
      }
  }
  </code></pre>
  
  <p><strong>File2.cs</strong></p>
  
  <pre><code>namespace B {
      public class Bar(){
      }
  }
  </code></pre>
  
  <p><strong>File3.cs</strong></p>
  
  <pre><code>namespace C {
      public class Bar(){
      }
  }
  </code></pre>
  
  <p>The issue is that I am not sure how to get to the Namespace value from where I am in the code right now. Any ideas? </p>