如何动态引用寻找另一个程序集的程序集

如何动态引用寻找另一个程序集的程序集

本文介绍了如何动态引用寻找另一个程序集的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉的问题的道歉-如果有人提出更好的建议,我们很乐意重新表述.

Apologies for the dodgy question - happy to rephrase if someone has a better suggestion.

我正在尝试通过动态调用属于另一个应用程序的程序集来创建对象.

I'm trying to create an object by dynamically invoking an assembly belonging to another application.

以下PowerShell代码对我来说效果很好:

The following PowerShell code is working nicely for me:

[Reflection.Assembly]::LoadFrom("C:\Program Files\Vendor\Product\ProductAPI.dll")
$bobject = new-object ProductAPI.BasicObject
$bobject.AddName("Some Name")

我正在努力用C#做同样的事情.根据StackOverflow上的其他帖子,我目前有以下内容:

I'm struggling to do the same thing in C#. Based on other posts on StackOverflow I currently have this:

System.Reflection.Assembly myDllAssembly =
System.Reflection.Assembly.LoadFile("C:\\Program Files\\Vendor\\Product\\ProductAPI.dll");

System.Type BasicObjectType = myDllAssembly.GetType("ProductAPI.BasicObject");

var basicObjectInstance = Activator.CreateInstance(BasicObjectType);

最后一行导致TargetInvocationException.

The final line results in a TargetInvocationException.

看来,BasicObject构造函数正在尝试调用AnotherObject(从同一文件夹中的AnotherObject.dll),但是找不到它.

It appears that the BasicObject constructor is trying to invoke AnotherObject (from AnotherObject.dll in the same folder) but can't find it.

有关如何解决此问题的任何提示?

Any tips on how to get around this?

推荐答案

如果在平常的地方,则需要手动指定查找方式.

If it can't find a dependent assembly in the usual places, you'll need to manually specify how to find them.

我知道的两种最简单的方法:

The two easiest ways I'm aware of for doing this:

  1. 预先手动加载相关程序集 Assembly.Load .

  1. manually load the dependent assemblies in advance withAssembly.Load.

处理正在加载域的AssemblyResolve事件具有其他程序集依赖性的程序集.

handle the AssemblyResolve event for the domain which is loading theassembly with additional assembly dependencies.

从本质上来说,这两者都要求您事先了解要尝试加载的程序集的依赖关系,但我认为这并不是一个大问题.

Both essentially require you to know the dependencies for the assembly you're trying to load in advance but I don't think that's such a big ask.

如果您选择第一个选项,则值得研究一下满载"负载与仅反射载荷.

If you go with the first option, it would also be worthwhile looking into the difference between a full Load and a reflection-only Load.

如果您愿意使用 2 (我建议这样做),您可以尝试使用类似的方法,它具有使用嵌套依赖项链的其他好处(例如MyLib.dll参考LocalStorage.dll参考Raven.Client.dll参考NewtonSoft.Json.dll),并且另外为您提供有关找不到哪些依赖项的信息:​​

If you would rather go with 2 (which I'd recommend), you can try something like this which has the added benefit of working with nested dependency chains (eg MyLib.dll references LocalStorage.dll references Raven.Client.dll references NewtonSoft.Json.dll) and will additionally give you information about what dependencies it can't find:

AppDomain.CurrentDomain.AssemblyResolve += (sender,args) => {

    // Change this to wherever the additional dependencies are located
    var dllPath = @"C:\Program Files\Vendor\Product\lib";

    var assemblyPath = Path.Combine(dllPath,args.Name.Split(',').First() + ".dll");

    if(!File.Exists(assemblyPath))
       throw new ReflectionTypeLoadException(new[] {args.GetType()},
           new[] {new FileNotFoundException(assemblyPath) });

    return Assembly.LoadFrom(assemblyPath);
};

这篇关于如何动态引用寻找另一个程序集的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:12