查找一组装配中的哪一个取决于装配X.

时间:2010-04-10 09:19:27

标签: .net

如果给出一个带有.Net程序集的文件夹,我怎么能快速找到这些文件夹取决于某些给定的程序集?

(用Reflector一个接一个地打开它们并不是我的想法)

3 个答案:

答案 0 :(得分:2)

您可以使用GetReferencedAssemblies()汇编方法在代码中执行此操作:

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);
                   });
}

答案 1 :(得分:1)

您是否尝试过使用Assembly.ReflectionOnlyLoad然后使用Assembly.GetReferencedAssemblies?查看引用的程序集以查看列表是否包含您所在的程序。

答案 2 :(得分:1)

我修改了@ Elisha的答案:

    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;
            });
    }