Forked from brianlow/FindConflictingReferences.cs
Last active
December 26, 2022 11:57
-
-
Save WaffleSouffle/bcc3eaebaa7a7cadcab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.IO; | |
using System.Reflection; | |
namespace FindConflictingReference | |
{ | |
public class Reference | |
{ | |
public AssemblyName Assembly { get; set; } | |
public AssemblyName ReferencedAssembly { get; set; } | |
} | |
public class FindConflictingReferenceFunctions | |
{ | |
static public IEnumerable<IGrouping<string, Reference>> FindReferencesWithTheSameShortNameButDiffererntFullNames(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; | |
} | |
static public List<Reference> GetReferencesFromAllAssemblies(List<Assembly> assemblies) | |
{ | |
var references = new List<Reference>(); | |
foreach (var assembly in assemblies) | |
{ | |
if (assembly == null) | |
continue; | |
foreach (var referencedAssembly in assembly.GetReferencedAssemblies()) | |
{ | |
references.Add(new Reference | |
{ | |
Assembly = assembly.GetName(), | |
ReferencedAssembly = referencedAssembly | |
}); | |
} | |
} | |
return references; | |
} | |
static public 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 => | |
{ | |
try | |
{ | |
Assembly asm = Assembly.LoadFile(file.FullName); | |
return asm; | |
} | |
catch (System.BadImageFormatException) | |
{ | |
return null; | |
} | |
}); | |
} | |
} | |
// Based on https://gist.github.com/brianlow/1553265 | |
class Program | |
{ | |
private static void FindConflicts(System.IO.TextWriter output, string path) | |
{ | |
var assemblies = FindConflictingReferenceFunctions.GetAllAssemblies(path); | |
var references = FindConflictingReferenceFunctions.GetReferencesFromAllAssemblies(assemblies); | |
var groupsOfConflicts = FindConflictingReferenceFunctions.FindReferencesWithTheSameShortNameButDiffererntFullNames(references); | |
foreach (var group in groupsOfConflicts) | |
{ | |
output.WriteLine("Possible conflicts for {0}:", group.Key); | |
foreach (var reference in group) | |
{ | |
output.WriteLine("{0} references {1}", | |
reference.Assembly.Name.PadRight(25), | |
reference.ReferencedAssembly.FullName); | |
} | |
output.WriteLine(); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
var paths = new List<string>(); | |
for (int argIter = 0; argIter < args.Length; ++argIter) | |
{ | |
string arg = args[argIter]; | |
if (arg.StartsWith("-")) | |
{ | |
if (arg.StartsWith("--")) | |
{ | |
// Long option. | |
} | |
else | |
{ | |
// Short option. | |
} | |
} | |
else if (arg.StartsWith("/")) | |
{ | |
// Windows option. | |
} | |
else | |
{ | |
paths.Add(arg); | |
} | |
} // Ends loop over arguments | |
if (paths.Count == 0) | |
{ | |
paths.Add(System.IO.Directory.GetCurrentDirectory()); | |
} | |
foreach (string path in paths) | |
{ | |
FindConflicts(System.Console.Out, path); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI I've created a full repo for this with a VS2015 csproj (to compile it).
https://github.com/collinsauve/FindConflictingReferences