Skip to content

Instantly share code, notes, and snippets.

@Insire
Forked from Cryental/getallrefs.cs
Created February 8, 2020 18:03
Show Gist options
  • Select an option

  • Save Insire/c0043bd02a069c735a56fac7cbbac0b0 to your computer and use it in GitHub Desktop.

Select an option

Save Insire/c0043bd02a069c735a56fac7cbbac0b0 to your computer and use it in GitHub Desktop.
Get All References From Project File (csproj)
private static List<string> GetAllReferences(string fullPath)
{
var logger = new ConsoleLogger(LoggerVerbosity.Quiet);
var manager = BuildManager.DefaultBuildManager;
var projectInstance = new ProjectInstance(fullPath);
var result = manager.Build(new BuildParameters {DetailedSummary = false, Loggers = new List<ILogger> {logger}}, new BuildRequestData(projectInstance, new[] {"ResolveProjectReferences", "ResolveAssemblyReferences"}));
var items1 = PrintResultItems(result, "ResolveProjectReferences");
var items2 = PrintResultItems(result, "ResolveAssemblyReferences");
var allItems = items1.ToList();
allItems.AddRange(items2);
return allItems;
}
private static string[] PrintResultItems(BuildResult result, string targetName)
{
var buildResult = result.ResultsByTarget[targetName];
var buildResultItems = buildResult.Items;
return buildResultItems.Length == 0 ? new string[0] { } : buildResultItems.Select(item => item.ItemSpec).ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment