Skip to content

Instantly share code, notes, and snippets.

@dagrooms52
Created June 24, 2016 17:59
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 dagrooms52/f5179304f759a1ff7d0332c87dad487c to your computer and use it in GitHub Desktop.
Save dagrooms52/f5179304f759a1ff7d0332c87dad487c to your computer and use it in GitHub Desktop.
Finds conflicting references in your solution. Original file: https://gist.github.com/brianlow/1553265
// Original: https://gist.github.com/brianlow/1553265
// Changes: Added multiple project finding;
// Added async await, brought my runtime from 10s to 3s
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
namespace HealthCare.Build.UnitTest
{
/// <summary>
/// Copied from https://gist.github.com/brianlow/1553265
/// Finds conflicting references in every assembly of the projects in the HealthCare solution
/// Check test output for the conflicting assemblies
/// Does not throw; it will always pass, since the output just shows possible conflicting assemblies
/// which are not always actionable
/// </summary>
[TestClass]
public class ReferenceTests
{
[TestMethod]
public async Task BuildTest_FindConflictingReferences()
{
var solnFolder = ""; // Your solution folder
var directories = Directory.GetDirectories(solnFolder);
var projectFolders = directories.Where(x => x.Contains("")); // Grab your project folders
foreach (var folder in projectFolders)
{
var targetFolder = $"{folder}\\bin\\x64\\Debug"; // or bin/debug
if (Directory.Exists(targetFolder))
{
var assemblies = await Task.FromResult(GetAllAssemblies(targetFolder));
var references = await Task.FromResult(GetReferencesFromAllAssemblies(assemblies));
var groupsOfConflicts = FindReferencesWithTheSameShortNameButDifferentFullNames(references);
string sectionHeader = $"-----{folder}-----";
Console.WriteLine(sectionHeader);
foreach (var group in groupsOfConflicts)
{
var padding = group.Max(x => x.ToString().Length);
Console.Out.WriteLine("Possible conflicts for {0}:", group.Key);
foreach (var reference in group)
{
Console.Out.WriteLine("\t{0} references {1}",
reference.Assembly.Name.PadRight(padding),
reference.ReferencedAssembly.FullName);
}
}
Console.WriteLine(string.Concat(Enumerable.Repeat("-", sectionHeader.Length)) + "\n\n");
}
}
}
private static IEnumerable<IGrouping<string, Reference>> FindReferencesWithTheSameShortNameButDifferentFullNames(List<Reference> references)
{
return from reference in references
group reference by reference.ReferencedAssembly.Name
into referenceGroup
where referenceGroup.ToList().Select(reference => reference.ReferencedAssembly.FullName).Distinct().Count() > 1
select referenceGroup;
}
private static List<Reference> GetReferencesFromAllAssemblies(List<Assembly> assemblies)
{
var references = new List<Reference>();
foreach (var assembly in assemblies)
{
foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
{
references.Add(new Reference
{
Assembly = assembly.GetName(),
ReferencedAssembly = referencedAssembly
});
}
}
return references;
}
private static List<Assembly> GetAllAssemblies(string path)
{
var files = new List<FileInfo>();
var directoryToSearch = new DirectoryInfo(path);
files.AddRange(directoryToSearch.GetFiles("*.dll", SearchOption.AllDirectories));
files.AddRange(directoryToSearch.GetFiles("*.exe", SearchOption.AllDirectories));
return files.ConvertAll(file => Assembly.LoadFile(file.FullName));
}
private class Reference
{
public AssemblyName Assembly { get; set; }
public AssemblyName ReferencedAssembly { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment