C# 解析 sln 文件

C# 解析 sln 文件我的项目,编码工具需要检测打开一个工程,获取所有项目。但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得sln文件的所有项目。

大家好,又见面了,我是你们的朋友全栈君。

我的项目,编码工具 需要检测打开一个工程,获取所有项目。

但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得sln文件的所有项目。

原先使用的方法dte.Solution.Projects但是放在文件夹的项目获取不到,所以使用堆栈提供的方法。

首先添加引用 Microsoft.Build 注意版本

这里写图片描述

然后把我三个类放到项目,其实放两个就好了,具体参见我的github

    public class Solution
    {
        //internal class SolutionParser
        //Name: Microsoft.Build.Construction.SolutionParser
        //Assembly: Microsoft.Build, Version=4.0.0.0

        static readonly Type s_SolutionParser;
        static readonly PropertyInfo s_SolutionParser_solutionReader;
        static readonly MethodInfo s_SolutionParser_parseSolution;
        static readonly PropertyInfo s_SolutionParser_projects;

        static Solution()
        {
            //s_SolutionParser_projects.GetValue()
            s_SolutionParser = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_SolutionParser != null)
            {
                s_SolutionParser_solutionReader = s_SolutionParser.GetProperty("SolutionReader", BindingFlags.NonPublic | BindingFlags.Instance);
                s_SolutionParser_projects = s_SolutionParser.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
                s_SolutionParser_parseSolution = s_SolutionParser.GetMethod("ParseSolution", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public List<SolutionProject> Projects { get; private set; }
        public List<SolutionConfiguration> Configurations { get; private set; }

        public Solution(string solutionFileName)
        {
            if (s_SolutionParser == null)
            {
                throw new InvalidOperationException("Can not find type 'Microsoft.Build.Construction.SolutionParser' are you missing a assembly reference to 'Microsoft.Build.dll'?");
            }
            var solutionParser = s_SolutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(null);
            using (var streamReader = new StreamReader(solutionFileName))
            {
                s_SolutionParser_solutionReader.SetValue(solutionParser, streamReader, null);
                s_SolutionParser_parseSolution.Invoke(solutionParser, null);
            }
            var projects = new List<SolutionProject>();
            var array = (Array)s_SolutionParser_projects.GetValue(solutionParser, null);
            for (int i = 0; i < array.Length; i++)
            {
                projects.Add(new SolutionProject(array.GetValue(i)));
            }
            this.Projects = projects;
            GetProjectFullName(solutionFileName);
            //Object cfgArray = //s_SolutionParser_configurations.GetValue
            // s_SolutionParser_projects.GetValue(solutionParser, null);
            //PropertyInfo[] pInfos = null;
            //pInfos = cfgArray.GetType().GetProperties();
            //int count = (int)pInfos[1].GetValue(cfgArray, null);

            //var configs = new List<SolutionConfiguration>();
            //for (int i = 0; i < count; i++)
            //{ 
   
            // configs.Add(new SolutionConfiguration(pInfos[2].GetValue(cfgArray, new object[] { i })));
            //}

            //this.Configurations = configs;
        }

        private void GetProjectFullName(string solutionFileName)
        {
            DirectoryInfo solution = (new FileInfo(solutionFileName)).Directory;
            foreach (var temp in Projects.Where
                //(temp=>temp.RelativePath.EndsWith("csproj"))
                (temp => !temp.RelativePath.Equals(temp.ProjectName))
            )
            {
                GetProjectFullName(solution, temp);
            }
        }

        private void GetProjectFullName(DirectoryInfo solution, SolutionProject project)
        {
            //Uri newUri =new Uri(,UriKind./*Absolute*/);
            //if(project.RelativePath)

            project.FullName = System.IO.Path.Combine(solution.FullName, project.RelativePath);
        }
    }

    [DebuggerDisplay("{ProjectName}, {RelativePath}, {ProjectGuid}")]
    public class SolutionProject
    {
        static readonly Type s_ProjectInSolution;
        static readonly PropertyInfo s_ProjectInSolution_ProjectName;
        static readonly PropertyInfo s_ProjectInSolution_RelativePath;
        static readonly PropertyInfo s_ProjectInSolution_ProjectGuid;
        static readonly PropertyInfo s_ProjectInSolution_ProjectType;

        static SolutionProject()
        {
            s_ProjectInSolution = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_ProjectInSolution != null)
            {
                s_ProjectInSolution_ProjectName = s_ProjectInSolution.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_RelativePath = s_ProjectInSolution.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_ProjectGuid = s_ProjectInSolution.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
                s_ProjectInSolution_ProjectType = s_ProjectInSolution.GetProperty("ProjectType", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string ProjectName { get; private set; }
        public string RelativePath { get; private set; }
        public string ProjectGuid { get; private set; }
        public string ProjectType { get; private set; }
        public string FullName { set; get; }

        public SolutionProject(object solutionProject)
        {
            this.ProjectName = s_ProjectInSolution_ProjectName.GetValue(solutionProject, null) as string;
            this.RelativePath = s_ProjectInSolution_RelativePath.GetValue(solutionProject, null) as string;
            this.ProjectGuid = s_ProjectInSolution_ProjectGuid.GetValue(solutionProject, null) as string;
            this.ProjectType = s_ProjectInSolution_ProjectType.GetValue(solutionProject, null).ToString();
        }
    }

    public class SolutionConfiguration
    {
        static readonly Type s_ConfigInSolution;
        static readonly PropertyInfo configInSolution_configurationname;
        static readonly PropertyInfo configInSolution_fullName;
        static readonly PropertyInfo configInSolution_platformName;

        static SolutionConfiguration()
        {
            s_ConfigInSolution = Type.GetType("Microsoft.Build.Construction.ConfigurationInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (s_ConfigInSolution != null)
            {
                configInSolution_configurationname = s_ConfigInSolution.GetProperty("ConfigurationName", BindingFlags.NonPublic | BindingFlags.Instance);
                configInSolution_fullName = s_ConfigInSolution.GetProperty("FullName", BindingFlags.NonPublic | BindingFlags.Instance);
                configInSolution_platformName = s_ConfigInSolution.GetProperty("PlatformName", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string configurationName { get; private set; }
        public string fullName { get; private set; }
        public string platformName { get; private set; }


        public SolutionConfiguration(object solutionConfiguration)
        {
            this.configurationName = configInSolution_configurationname.GetValue(solutionConfiguration, null) as string;
            this.fullName = configInSolution_fullName.GetValue(solutionConfiguration, null) as string;
            this.platformName = configInSolution_platformName.GetValue(solutionConfiguration, null) as string;
        }
    }

注意要引用

     using System;
     using System.Collections.Generic;
     using System.Diagnostics;
     using System.IO;
     using System.Linq;
     using System.Reflection;

稍微说下上面代码,主要用的是反射。

用反射获得解析 sln 的 s_SolutionParser_parseSolution 他可以获得所有项目。

但是获得的项目路径是相对的,于是使用C# 相对路径转绝对路径,可以转换项目路径。

使用

输入工程文件名就好,输入工程名,会自动获得所有项目。

 Solution solution = new Solution(工程文件路径);

获得工程文件的所有项目

  foreach (var temp in solution.Projects)
  {


  }

代码:https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da

参见:https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project.propertygroups%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130580.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 我的中国问题

    我的中国问题

    2022年1月15日
    46
  • WebSocket :用WebSocket实现推送你必须考虑的几个问题

    WebSocket :用WebSocket实现推送你必须考虑的几个问题目录:目录WebSocket简介项目背景硬件环境及客户端支持本文研究内容基于javaxwebsocket服务端代码源码后续补充git连接客户端代码问题探索如何做到支持几千个client同时在线人数后续1.WebSocket简介WebSocket_百度百科2.项目背景、硬件环境及客户端支持本项目通过WebSocket实现同时在线用户量5k推送服务器(可内网运行)。且可实时查看用户在线状

    2022年7月15日
    20
  • html背景图片拉伸解决办法

    html背景图片拉伸解决办法html背景图片拉伸解决办法body{background-size:100%100%;//关键代码,直接拉伸背景图background-image:url(“img/99.jpg”);//背景图导入background-repeat:no-repeat;//不重复拉伸background-attachment:fixed;//此代码用于页面内容超出屏幕,

    2022年9月28日
    1
  • 不是单组分组函数

    不是单组分组函数问题:一:SELECT tablespace_name, SUM(bytes) freeFROM dba_free_space不是单组分组函数原因: 1、如果程序中使用了分组函数,则有两种情况可以使用:程序中存在group by,并指定了分组条件,这样可以将分组条件一起查询出来改为:  SELECT tablespace_name, SUM(bytes) freeFROM dba_free_spa…

    2022年6月30日
    35
  • 数字图像处理基本知识

    数字图像处理基本知识二 数字图像处理的概念 1 什么是图像 图 是物体投射或反射光的分布 像 是人的视觉系统对图的接受在大脑中形成的印象或反映 nbsp 是客观和主观的结合 2 数字图像是指由被称作象素的小块区域组成的二维矩阵 将 nbsp nbsp 物理图象行列划分后 每个小块区域称为像素 pixel nbsp nbsp nbsp 每个像素包括两个属性 位置和灰度 对于单色即灰度图像而言 每个象素的亮度用一个数值来表示

    2025年8月27日
    0
  • 分布式系统设计权衡之CAP(一致性,可用性,分区容错性)[通俗易懂]

    分布式系统设计权衡之CAP(一致性,可用性,分区容错性)[通俗易懂]https://blog.csdn.net/Sun_P0/article/details/50221787写在最前:1.为什么学习并记录分布式设计理念一系列相关的东西在日常工作中系统设计评审的时候,经常会有一些同事抛出一些概念,高可用性,一致性等等字眼,他们用这些最基本的概念去反驳系统最初的设计,但是很多人理解的可用性,一致性等等问题,都是自己拍脑袋想的,或者根本和最原始表达的意思就不…

    2022年7月25日
    12

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号