Finding which of a set of assemblies depends on assembly X
How can I quickly find, given a folder with .Net assemblies, which of these depends on som开发者_如何学JAVAe given assembly?
(Opening them up one by one with Reflector is not what I had in mind)
You can do it in code using GetReferencedAssemblies()
method of assembly:
public void GetDependentAssemblies(Assembly assembly)
{
var assembliesPaths = new[] {
@"c:\dlls\assembly1.dll",
@"c:\dlls\assembly2.dll"
};
IEnumerable<Assembly> dependantAssemblies = assembliesPaths
.Select(path => Assembly.ReflectionOnlyLoadFrom(path))
.Where(folderAssembly =>
{
var fullNames = folderAssembly.GetReferencedAssemblies()
.Select(name => name.FullName);
return fullNames.Contains(assembly.GetName().FullName);
});
}
Have you tried using Assembly.ReflectionOnlyLoad
and then Assembly.GetReferencedAssemblies
? Look through the referenced assemblies to see if the list includes the one you're after.
I modified @Elisha's answer just a bit:
public static IEnumerable<Assembly> GetDependentAssemblies(string assemblyName, string assembliesPath)
{
var assembliesPaths = Directory.GetFiles(assembliesPath, "*.dll");
return assembliesPaths
.Select(Assembly.ReflectionOnlyLoadFrom)
.Where(folderAssembly =>
{
var fullNames = folderAssembly.GetReferencedAssemblies()
.Select(name => name.FullName);
return fullNames.Where(x =>
x.ToLower().Contains(assemblyName.ToLower())).Count() > 0;
});
}
精彩评论