我认为您的方式应该通过正常工作,但为了从解决方案文件中提取信息,我们通过访问“Microsoft.Build”命名空间的内部技术使用以下方法。由于相应的框架函数标记为internal
,因此有必要对其进行“破解”。因此使用反射的复杂方式。(这是我们在互联网上找到的其他代码片段的浓缩摘录 - 也在 SO 中)
public struct ProjectData
{
public string Name;
public string Guid;
public string Path;
}
public static List<ProjectData> GetProjectsInSolution(string pathToSolutionFile)
{
Type parserType = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
ConstructorInfo constructorInfo = parserType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
object solutionParser = constructorInfo.Invoke(null);
PropertyInfo solutionFileProperty = parserType.GetProperty("SolutionFile", BindingFlags.NonPublic | BindingFlags.Instance);
solutionFileProperty.SetValue(solutionParser, pathToSolutionFile);
MethodInfo parserMethod = parserType.GetMethod("ParseSolutionFile", BindingFlags.NonPublic | BindingFlags.Instance);
parserMethod.Invoke(solutionParser, null);
PropertyInfo projectInfo = parserType.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
object[] projects = projectInfo.GetValue(solutionParser) as object[];
Type projectType = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
PropertyInfo nameInfo = projectType.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo guidInfo = projectType.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo pathInfo = projectType.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
List<ProjectData> projectData = new List<ProjectData>();
if (projects != null)
{
foreach (object project in projects)
{
projectData.Add(new ProjectData()
{
Name = nameInfo.GetValue(project) as string,
Guid = guidInfo.GetValue(project) as string,
Path = pathInfo.GetValue(project) as string
});
}
}
return projectData;
}