Skip to content

Instantly share code, notes, and snippets.

@becdetat
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save becdetat/9a5a336d82b51ac0b564 to your computer and use it in GitHub Desktop.
Save becdetat/9a5a336d82b51ac0b564 to your computer and use it in GitHub Desktop.
Wrapper classes for reading solutions and projects, taken from http://stackoverflow.com/a/4634505/149259
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace MyProject.Conventions.SolutionConventions
{
public class Solution
{
//internal class SolutionParser
//Name: Microsoft.Build.Construction.SolutionParser
//Assembly: Microsoft.Build, Version=4.0.0.0
private static readonly Type s_SolutionParser;
private static readonly PropertyInfo s_SolutionParser_solutionReader;
private static readonly MethodInfo s_SolutionParser_parseSolution;
private static readonly PropertyInfo s_SolutionParser_projects;
static Solution()
{
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 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)));
}
Projects = projects;
}
public List<SolutionProject> Projects { get; private set; }
}
}
using System;
using System.Diagnostics;
using System.Reflection;
namespace MyProject.Conventions.SolutionConventions
{
[DebuggerDisplay("{ProjectName}, {RelativePath}, {ProjectGuid}")]
public class SolutionProject
{
private static readonly Type s_ProjectInSolution;
private static readonly PropertyInfo s_ProjectInSolution_ProjectName;
private static readonly PropertyInfo s_ProjectInSolution_RelativePath;
private static readonly PropertyInfo s_ProjectInSolution_ProjectGuid;
private 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 SolutionProject(object solutionProject)
{
ProjectName = s_ProjectInSolution_ProjectName.GetValue(solutionProject, null) as string;
RelativePath = s_ProjectInSolution_RelativePath.GetValue(solutionProject, null) as string;
ProjectGuid = s_ProjectInSolution_ProjectGuid.GetValue(solutionProject, null) as string;
ProjectType = s_ProjectInSolution_ProjectType.GetValue(solutionProject, null).ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment