2018-2-13-C#-解析-sln-文件
title | author | date | CreateTime | categories |
---|---|---|---|---|
C# 解析 sln 文件
|
lindexi
|
2018-2-13 17:23:3 +0800
|
2018-2-13 17:23:3 +0800
|
C#
|
我的项目,编码工具 需要检测打开一个工程,获取所有项目。
但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得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)
{ }
<script src="https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da.js"></script>
代码:https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da
http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files
2018-2-13-C#-解析-sln-文件的更多相关文章
- C# 解析 sln 文件
我的项目,编码工具 需要检测打开一个工程,获取所有项目. 但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得sln文件的所有项目. 原先使用的方法d ...
- CSharpGL(9)解析OBJ文件并用CSharpGL渲染
CSharpGL(9)解析OBJ文件并用CSharpGL渲染 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo ...
- Android开发学习---使用XmlPullParser解析xml文件
Android中解析XML的方式主要有三种:sax,dom和pull关于其内容可参考:http://blog.csdn.net/liuhe688/article/details/6415593 本文将 ...
- OAF_文件系列5_实现OAF解析XML文件javax.xml.parsers(案例)
20150729 Created By BaoXinjian
- 解析XML文件的几种常见操作方法—DOM/SAX/DOM4j
解析XML文件的几种常见操作方法—DOM/SAX/DOM4j 一直想学点什么东西,有些浮躁,努力使自己静下心来看点东西,哪怕是回顾一下知识.看到了xml解析,目前我还没用到过.但多了解一下,加深点记忆 ...
- Java环境解析apk文件信息
概述:Java解析apk文件,获取apk文件里的包名,版本号,图标文件等; 功能:可以提供给windows和linux平台使用; 原理:利用aapt.exe或者aapt这些anroid平台解析apk文 ...
- 解析 MACH_O 文件
现在做iOS开发的挺多,了解一下在苹果平台上程序运行的原理 解析 MACH_O 文件 这篇文章描述了如何解析 Mach-O 文件并稍微解释了一下它的格式.这不是一份权威指南,不过当你不知从何开始时,它 ...
- java解析properties文件
在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...
- 解析XML文件示例
项目中要解析Xml文件,于是在工程里找了下前人写例子. 1,SAX(基于事件,效率高,使用声明加载什么). public class MVCConfig { private static MVCCon ...
- [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found
google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...
随机推荐
- 解决Apache日志"internal dummy connection"方法
最近查看服务器中apache日志,发现有大量的 OPTIONS * HTTP/1.0" 200 - "-" "Apache (internal dummy co ...
- 只在需要的时候 Polyfill 你的 JavaScript 代码
本文转载自 Pascal Klau,他是一名来自德国南部的实习生,他讨厌不必要的 HTTP 请求,也不爱吃西兰花.Pascal 将说明使用 polyfill 服务的一种方式,在这种方式下你可能可以完全 ...
- @spoj - ADAMOLD@ Ada and Mold
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 N 的序列 A,将其划分成 K + 1 段,划分 ...
- ZOJ 3956 Course Selection System [01背包]
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...
- Extended Traffic
题目链接 题意:有n个路口,m条通路,如果经过一条路则会得到(终点 - 起点)^3的权值,求从1点到其他点的最小权值,如果权值小于3或无法到达输出'?'. 题解:因为权值可能为负,所以用SPFA来解题 ...
- git push的时候每次都要输入用户名和密码的问题解决
换了个ssh key,发现每次git push origin master的时候都要输入用户名和密码 原因是在添加远程库的时候使用了https的方式..所以每次都要用https的方式push到远程库 ...
- 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略
把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811) spring-mybatis.xml文件中配 ...
- 洛谷P2504 [HAOI2006]聪明的猴子
#include<bits/stdc++.h> using namespace std; ; ; int n,m,k,ans; double Max; int monkey[maxn]; ...
- Python深入:setuptools简介
Setuptools是Python Distutils的加强版,使开发者构建和发布Python包更加容易,特别是当包依赖于其他包时.用setuptools构建和发布的包与用Distutils发布的包是 ...
- Android教程-03 常见布局的总结
常见的布局 视频建议采用超清模式观看, 欢迎点击订阅我的优酷 Android的图形用户界面是由多个View和ViewGroup构建出来的.View是通用的UI窗体小组件,比如按钮(Button)或者文 ...