Forked from WaffleSouffle/FindConflictingReferences.cs
Last active
September 22, 2017 07:13
-
-
Save automatonic/9ea91ce2b15bef5d6e30 to your computer and use it in GitHub Desktop.
LinqPad version that runs in the location of the .Linq file http://share.linqpad.net/e9ne3q.linq
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
<Query Kind="Program"> | |
<Namespace>System.Collections.Generic</Namespace> | |
<Namespace>System.IO</Namespace> | |
<Namespace>System.Linq</Namespace> | |
<Namespace>System.Reflection</Namespace> | |
</Query> | |
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 | |
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>(); | |
paths.Add(Path.GetDirectoryName(Util.CurrentQueryPath)); | |
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