English 中文(简体)
assembly.GetReferencedAssemblys返回“.exe”依赖项时会怎样?
原标题:What when assembly.GetReferencedAssemblies returns ".exe" dependency?

有很多示例如何从一些程序集中加载所有依赖项,例如:

var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
foreach (var assemblyName in assembly.GetReferencedAssemblies()) {
  try {
    Assembly.ReflectionOnlyLoad(assemblyName.FullName);
  } catch {
    Assembly.ReflectionOnlyLoadFrom(Path.Combine(Path.GetDirectoryName(assemblyPath), assemblyName.Name + ".dll"));
  }
}

但是如果其中一个依赖项不是“.dll”而是“.exe”呢?我是否需要该“.exe”程序集再次调用foreach循环中的递归GetReferencedAssemblies()?是否存在获得循环依赖性的危险?

br, Milan

问题回答

You should be able to use the exe just like any other dll. It just has this extra bonus that it can be executed standalone.

作为dll的程序集可以和exe一样引用其他程序集;从这个角度看,他们之间没有区别。

如前所述,在引用方面,您可以像对待.dll一样对待.exe。

至于担心循环引用,如果有循环引用,这些库会编译吗?只是一个想法。

下面是一些快速递归代码,用于获取所有依赖程序集:

    private static IEnumerable<Assembly> GetAllDependencies(Assembly assembly)
    {
        var dict = new Dictionary<string, AssemblyName>();
        dict.Add(assembly.GetName().FullName, assembly.GetName());
        dict = GetAllDependenciesRecursive(assembly.GetName(), dict);
        return dict.Select(d => Assembly.Load(d.Value)).ToArray();
    }

    private static Dictionary<string, AssemblyName> GetAllDependenciesRecursive(AssemblyName assemblyName, Dictionary<string, AssemblyName> existingRefList)
    {
        var assembly = Assembly.Load(assemblyName);
        List<AssemblyName> a = assembly.GetReferencedAssemblies().ToList();
        foreach (var refAssemblyName in a)
        {
            if (!existingRefList.ContainsKey(refAssemblyName.FullName))
            {
                existingRefList.Add(refAssemblyName.FullName, refAssemblyName);
                existingRefList = GetAllDependenciesRecursive(refAssemblyName, existingRefList);
            }
        }
        return existingRefList;
    }

GetReferencedAssemblies()不会返回整个依赖关系树(dll或exe没有区别),因此您需要一个遍历该树的解决方案。请参阅本文http://msdn.microsoft.com/en-us/magazine/cc163641.aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签