加载库的相对路径?

加载库的相对路径?

问题描述:

我得到了以下编译文件夹(简体):

I got the following compilation folders(simplified) :

D:\MySolution\Project1\bin\Debug
D:\MySolution\Project2\bin\Debug

我的Project1Project2上有一个引用,但是在代码中,我必须从Project1加载类,因此我使用了有效的代码:

My Project1 has a reference on Project2 but in the code I have to load a class from Project1 so I used this code that works :

var project2ref = Assembly.LoadFrom(@"..\..\..\Project1\bin\Debug\Project1.dll")
                                .CreateInstance("MyNamespace.MyClass");

这在VS中有效,但是当我使用软件的安装版本时会崩溃... 相对路径有哪些替代方法?

This works in VS but it crashes when I use an install version of my software... What are the alternatives for the relative path ?

构建解决方案时,将project1的dll放入project2的bin文件夹中. 因此,您可以通过检索project1.dll的汇编路径来获得project2.dll的引用.下面的代码将在project1内部执行,并将获取包含两个dll的项目的bin路径.

You will have the dll of project1 into the bin folder of project2 when you'll build the solution. So, you can get a reference of the project2.dll by retrieving the assembly path of project1.dll. The below code will be executed inside the project1 and will get the bin path of the project who will contains both the dll.

System.Reflection.Assembly.GetExecutingAssembly().Location