C# 获取解决方案中包含的所有类名
有没有一种方法可以让所有包含的类都包含在解决方案中? 至少所有类名,例如C# 获取解决方案中包含的所有类名,c#,winforms,C#,Winforms,有没有一种方法可以让所有包含的类都包含在解决方案中? 至少所有类名,例如classname.cs 但问题是创建代码,以便在其他解决方案中查找includedClass。 这意味着不是我编写代码的解决方案,而是另一个解决方案 以下是我到目前为止研究的内容: 但是我仍然不知道如何通过path(of.sln)获得它?或者我如何实现它。 提前谢谢 您可以首先读取SolutionIn文件并获取包含的项目 如果您使用记事本打开解决方案文件,您将注意到项目如下所示 Project(“{00000000-00
classname.cs
但问题是创建代码,以便在其他解决方案中查找includedClass。
这意味着不是我编写代码的解决方案,而是另一个解决方案
以下是我到目前为止研究的内容:
但是我仍然不知道如何通过path(of.sln)获得它?或者我如何实现它。
提前谢谢 您可以首先读取SolutionIn文件并获取包含的项目
如果您使用记事本打开解决方案文件,您将注意到项目如下所示
Project(“{00000000-0000-0000-0000-000000000000}”)=“项目名称”、“项目路径”、“{00000000-0000-0000-0000-000000000000000000}”
例如,您可以使用reguler expretion获取项目列表
获得项目列表后,您可以读取项目文件并生成所有代码文件的列表
项目文件是xml格式的
string solutionFile="the solution file";FileInfo fInfo = new FileInfo(solutionFile);
string solutionText = File.ReadAllText(solutionFile);
Regex reg = new Regex("Project\\(\"[^\"]+\"\\) = \"[^\"]+\", \"([^\"]+)\", \"[^\"]+\"");
MatchCollection mc = reg.Matches(solutionText);
List<string> files = new List<string>();
foreach (Match m in mc)
{
string project_file = m.Groups[1].Value;
project_file = System.IO.Path.Combine(fInfo.Directory.FullName, project_file);
if (System.IO.File.Exists(project_file))
{
string project_path = new FileInfo(project_file).DirectoryName;
XmlDocument doc = new XmlDocument();
doc.Load(project_file);
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003");
System.Xml.XmlNodeList list = doc.ChildNodes[1].SelectNodes("//ms:ItemGroup/ms:Compile", ns);
foreach (XmlNode node in list)
{
files.Add(Path.Combine(project_path, node.Attributes["Include"].InnerText));
}
}
}
你知道怎么做吗@Aladinhdabe不客气,我已经用你所说的确定包含类的代码更新了答案,但听起来你真正想要的是包含的C#源文件。没有规则规定源文件的名称必须与类的名称匹配(尽管这是一个很好的经验法则),事实上,一个源文件可能包含多个类,一个类可能在多个源文件中实现。
XmlNodeList references = doc.ChildNodes[1].SelectNodes("//ms:ItemGroup/ms:Reference", ns);
foreach (XmlNode node in references)
{
string name_space = node.Attributes["Include"].InnerText;
string name_space_path;
XmlNode nHintPath = node.SelectSingleNode("//ms:HintPath", ns);
if (nHintPath != null)
{
name_space_path = nHintPath.InnerText;
if (!Path.IsPathRooted(name_space_path))
{
name_space_path = Path.Combine(project_path, name_space_path);
}
}
}