C#ファイルの先頭にロードされた名前空間の名前を取得するには? たとえば、以下の 6 つの名前空間名を取得します。
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;
namespace MyNM
{
    class MyClass{}
}
    C#ファイルの先頭にロードされた名前空間の名前を取得するには? たとえば、以下の 6 つの名前空間名を取得します。
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;
namespace MyNM
{
    class MyClass{}
}
    C#ファイルの先頭にロードされた名前空間の名前を取得するには? たとえば、以下の 6 つの名前空間名を取得します。
自分でファイルを解析する (または Roslyn CTP のようなものを使用して C# ファイルを解析する) 以外はできません。
名前空間は「ロード」されません。コンパイル時に、適切な型名を解決するためにのみ使用されます。
Assembly.GetReferencedAssembliesを使用して、アセンブリ(つまり、プロジェクト)によって参照されるアセンブリを取得できますが、これはアセンブリ全体の参照セットであり、特定のファイルに含まれるディレクティブを使用する名前空間とは明らかに異なります。
これにより、実行されたアセンブリによって参照されるすべてのアセンブリが返されます。
それでも、これは特定のファイル内で使用されている名前空間だけを返すわけではありません - これは実行時には不可能です。
var asms = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (var referencedAssembly in asms)
{
    Console.WriteLine(referencedAssembly.Name);
}
ただし、技術的には、現在のコードを含むファイルの名前がわかっている場合は、実行時にファイルを読み取って「使用中」を抽出するだけです。
編集
これを行う方法は次のとおりです。
public static IEnumerable<string> GetUsings()
{
    // Gets the file name of the caller
    var fileName = new StackTrace(true).GetFrame(1).GetFileName();
    // Get the "using" lines and extract and full namespace
    return File.ReadAllLines(fileName)
               .Select(line => Regex.Match(line, "^\\s*using ([^;]*)"))
               .Where(match => match.Success)
               .Select(match => match.Groups[1].Value);
}
    構文要素を導出するには、ファイルを解析する必要があります。上記のように、外部参照に System.Reflections を使用するか、以下のように新しいRoslynコンパイラ サービスを使用できます。
string codeSnippet = @"using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;
namespace MyNM
{
    class MyClass{}
}";
            //APPROACH:
            //Get using statements in the code snippet from the syntax tree
            //find qualified name(eg:System.Text..) in the using directive 
            SyntaxTree tree = SyntaxTree.ParseCompilationUnit(codeSnippet);
            SyntaxNode root = tree.GetRoot();
            IEnumerable<UsingDirectiveSyntax> usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>();
            foreach(UsingDirectiveSyntax usingDirective in usingDirectives){
                NameSyntax ns = usingDirective.Name;
                Console.WriteLine(ns.GetText());
            }
注: コードでは古いバージョンの Roslyn API を使用しています。Roslyn はまだ CTP であるため、将来壊れる可能性があります。