using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using NUnit.Framework; | |
namespace MyProject | |
{ | |
[TestFixture] | |
public class UtilityTest | |
{ | |
[Test] | |
public void FindConflictingReferences() | |
{ | |
var assemblies = GetAllAssemblies(@"E:\dev\myapp\myproject\bin\debug"); | |
var references = GetReferencesFromAllAssemblies(assemblies); | |
var groupsOfConflicts = FindReferencesWithTheSameShortNameButDiffererntFullNames(references); | |
foreach (var group in groupsOfConflicts) | |
{ | |
Console.Out.WriteLine("Possible conflicts for {0}:", group.Key); | |
foreach (var reference in group) | |
{ | |
Console.Out.WriteLine("{0} references {1}", | |
reference.Assembly.Name.PadRight(25), | |
reference.ReferencedAssembly.FullName); | |
} | |
} | |
} | |
private 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; | |
} | |
private 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 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; } | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
good job!, found this very helpful.. |
This comment has been minimized.
This comment has been minimized.
+1 nice one |
This comment has been minimized.
This comment has been minimized.
Worked like a charm. Thanks. |
This comment has been minimized.
This comment has been minimized.
Fantastic! |
This comment has been minimized.
This comment has been minimized.
Thank you so much |
This comment has been minimized.
This comment has been minimized.
This does not work for me. I get a
|
This comment has been minimized.
This comment has been minimized.
Hi |
This comment has been minimized.
This comment has been minimized.
The BadImageFormatException is because this utility doesn't consider the possibility of non .net images being in the bin folders, which is highly possible. You can put native dll's in the bin folder and load them with DllImport calls. Just change some lines,
And
|
This comment has been minimized.
This comment has been minimized.
Forked, incorporated ryios changes, made into a short program accepting directory paths on the command line. |
This comment has been minimized.
This comment has been minimized.
This is great!! Also it provides a template for any other projects you might be interested in involving dlls and references etc... For example, I am thinking of writing a project to find all unused references (no small task X( ) |
This comment has been minimized.
This comment has been minimized.
FYI I've created a full repo for this with a VS2015 csproj (to compile it). |
This comment has been minimized.
This comment has been minimized.
Sorry for stupid question but how to use this tool ?I want to see conflicting references in my project but how exactly to use it,attach to what process ?When i try to attach to my project process it say unable to attach to the process,debugger already attached. |
This comment has been minimized.
This comment has been minimized.
Would be nice for us normal people to have some documentation for how to use this. |
This comment has been minimized.
This comment has been minimized.
Same here. Could we have some instructions on how to use this unit please. Thanks :) |
This comment has been minimized.
This comment has been minimized.
awesome works great... |
This comment has been minimized.
This comment has been minimized.
@deepdrunk; either create a unit test proj in your solution and copy paste it to a test class there or just use either of the programs that @WaffleSouffle or @collinsauve created. |
This comment has been minimized.
This comment has been minimized.
Here is an XUnit Version with ryios's changes incorporated:
|
This comment has been minimized.
This comment has been minimized.
I updated this to make it a little faster (and find all project files in the solution). It uses async await to not block your other tests. https://gist.github.com/dagrooms52/f5179304f759a1ff7d0332c87dad487c |
This comment has been minimized.
This comment has been minimized.
Thank u @colinsauve for the project! Works good and helped me pinpoint the troubling dll. (I found that removing that particular dll from the project solved my problem) |
This comment has been minimized.
This comment has been minimized.
This doesn't cover cases where the reference is nested in another assembly, does it? Consider the following case:
I'm getting the "Found conflicts between different versions..." warning and I believe this scenario is the reason for it, but I don't think this test output includes this level of references. How can we modify it so that it searches references of references? |
This comment has been minimized.
see http://stackoverflow.com/questions/17806/warning-found-conflicts-between-different-versions-of-the-same-dependent-assemb