获取解决方案中所有包含的类名

本文关键字:包含 解决方案 获取 | 更新日期: 2023-09-27 18:32:15

有没有办法在解决方案中获得所有包含的类?至少所有类名,例如classname.cs

但问题是创建用于查找包含类in other solution的代码。意味着不是我正在做代码的解决方案,而是另一个解决方案.

以下是我到目前为止的研究:

分析 Visual Studio 解决方案文件

但是我仍然对如何通过路径(.sln)获取它感到困惑? 或者我如何实现它.提前感谢!

获取解决方案中所有包含的类名

您可以做的是首先读取 solutiin 文件并获取包含的项目如果使用记事本打开解决方案文件,您会注意到该项目被列为 folow

Project("{00000000-0000-0000-0000-000000000000}") = "project name", "project path", "{00000000-0000-0000-0000-000000000000}"

因此,例如,您可以使用调节器表达式来获取项目列表。获取项目列表后,可以读取项目文件并 GE 所有代码文件的列表项目文件采用 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));
                }
            }
        }

参考更新:可以使用此代码读取引用。

               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);
                        }
                    }
                }