国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# 解析 sln 文件

2019-11-08 18:52:08
字體:
來源:轉載
供稿:網友

我的項目,編碼工具 需要檢測打開一個工程,獲取所有項目。

但是發現原來的方法,如果存在文件夾,把項目放在文件夾中,那么是無法獲得項目,于是我就找了一個方法去獲得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 ),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布。如有任何疑問,請與我聯系。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇雄县| 宁晋县| 无为县| 潢川县| 夏津县| 通化市| 虹口区| 恩平市| 秭归县| 古交市| 桐城市| 平塘县| 林周县| 宁强县| 聂拉木县| 南岸区| 襄樊市| 满洲里市| 苏尼特左旗| 康平县| 富平县| 灵宝市| 苏尼特右旗| 建昌县| 儋州市| 清苑县| 韩城市| 武平县| 荥阳市| 萍乡市| 吕梁市| 迁安市| 铅山县| 申扎县| 宁津县| 阿克苏市| 墨竹工卡县| 深水埗区| 都兰县| 肇东市| 会理县|