C# 如何在开始时加载名称空间的名称

C# 如何在开始时加载名称空间的名称,c#,namespaces,C#,Namespaces,如何获取在C#文件开头加载的名称空间的名称?例如,获取下面的六个名称空间名称 using System; using System.CodeDom; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Text; using System.Reflection; namespace MyNM { class MyClass{} } 这将返回已执行程序集的所有程序集引用 不过,这不会只返回特定文件中使用

如何获取在C#文件开头加载的名称空间的名称?例如,获取下面的六个名称空间名称

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;

namespace MyNM
{
    class MyClass{}
}

这将返回已执行程序集的所有程序集引用

不过,这不会只返回特定文件中使用的名称空间,这在运行时是不可能的

var asms = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

foreach (var referencedAssembly in asms)
{
    Console.WriteLine(referencedAssembly.Name);
}
不过,从技术上讲,如果您知道包含当前代码的文件名,只需在运行时读取该文件并提取“using”

编辑

这样做的方法是:

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);
}
公共静态IEnumerable GetUsings()
{
//获取调用方的文件名
var fileName=new StackTrace(true).GetFrame(1).GetFileName();
//获取“using”行并提取和完整名称空间
返回文件.ReadAllLines(文件名)
.Select(line=>Regex.Match(使用([^;]*)”)的行“^\\s*)
.Where(匹配=>match.Success)
.Select(match=>match.Groups[1].Value);
}

此操作返回已执行程序集的所有程序集引用

不过,这不会只返回特定文件中使用的名称空间,这在运行时是不可能的

var asms = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

foreach (var referencedAssembly in asms)
{
    Console.WriteLine(referencedAssembly.Name);
}
不过,从技术上讲,如果您知道包含当前代码的文件名,只需在运行时读取该文件并提取“using”

编辑

这样做的方法是:

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);
}
公共静态IEnumerable GetUsings()
{
//获取调用方的文件名
var fileName=new StackTrace(true).GetFrame(1).GetFileName();
//获取“using”行并提取和完整名称空间
返回文件.ReadAllLines(文件名)
.Select(line=>Regex.Match(使用([^;]*)”)的行“^\\s*)
.Where(匹配=>match.Success)
.Select(match=>match.Groups[1].Value);
}
如何获取在C#文件开头加载的名称空间的名称?例如,获取下面的六个名称空间名称

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;

namespace MyNM
{
    class MyClass{}
}
除了自己解析文件(或者使用类似于Roslyn CTP的东西来解析C#文件)之外,您不能这样做

名称空间不是“加载的”——它们仅由编译人员在编译时用于解析适当的类型名

可以使用获取程序集(即:project)引用的程序集,但这是一组程序集范围的引用,与使用特定文件中包含的指令的命名空间截然不同

如何获取在C#文件开头加载的名称空间的名称?例如,获取下面的六个名称空间名称

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;

namespace MyNM
{
    class MyClass{}
}
除了自己解析文件(或者使用类似于Roslyn CTP的东西来解析C#文件)之外,您不能这样做

名称空间不是“加载的”——它们仅由编译人员在编译时用于解析适当的类型名


您可以使用获取程序集(即:project)引用的程序集,但这是一组程序集范围的引用,与使用特定文件中包含的指令的命名空间截然不同。

您必须解析该文件以派生语法元素。如上所述,您可以使用System.Reflections作为外部引用,也可以使用新的

string code snippet=@”使用系统;
使用System.CodeDom;
使用System.CodeDom.Compiler;
使用Microsoft.CSharp;
使用系统文本;
运用系统反思;
名称空间MyNM
{
类MyClass{}
}";
//方法:
//使用语法树中的代码段中的语句获取
//在using指令中查找限定名称(例如:System.Text..)
SyntaxTree-tree=SyntaxTree.ParseCompilationUnit(代码段);
SyntaxNode root=tree.GetRoot();
IEnumerable usingdirections=root.genderantNodes().OfType();
foreach(在usingdirections中使用directivesyntax usingDirective){
NameSyntax ns=usingDirective.Name;
Console.WriteLine(ns.GetText());
}

注意:该代码使用旧版本的Roslyn API。由于Roslyn仍然是一个CTP,它将来可能会崩溃。

您必须解析该文件以派生语法元素。如上所述,您可以使用System.Reflections作为外部引用,也可以使用新的

string code snippet=@”使用系统;
使用System.CodeDom;
使用System.CodeDom.Compiler;
使用Microsoft.CSharp;
使用系统文本;
运用系统反思;
名称空间MyNM
{
类MyClass{}
}";
//方法:
//使用语法树中的代码段中的语句获取
//在using指令中查找限定名称(例如:System.Text..)
SyntaxTree-tree=SyntaxTree.ParseCompilationUnit(代码段);
SyntaxNode root=tree.GetRoot();
IEnumerable usingdirections=root.genderantNodes().OfType();
foreach(在usingdirections中使用directivesyntax usingDirective){
NameSyntax ns=usingDirective.Name;
Console.WriteLine(ns.GetText());
}

注意:该代码使用旧版本的Roslyn API。它将来可能会崩溃,因为Roslyn仍然是一个CTP。

我尝试过Assembly.getExecutionGassembly().getReferenceAssembly(),但如果在上下文中根本不使用这些名称空间中的类,它会错过一些名称空间。但是,在我的问题中,我希望使用相同的编码环境。@KenS我已经实现了一种方法,使用当前文件中的s获取实际的
。请参见编辑!:)我刚注意到。匹配的字符串在我看来非常棒。:)我试试这个。感谢您尝试了Assembly.GetExecutionGassembly().GetReferencedAssembly(),但是如果在上下文中根本不使用这些名称空间中的类,那么它会错过一些名称空间。但是,在我的问题中,我希望使用相同的编码环境。@KenS我已经实现了一种方法,使用当前文件中的
s获取实际的
。请参见编辑!:)我只是