确定装配位置

发布于 2024-11-17 03:16:35 字数 518 浏览 2 评论 0原文

我有一个名为 A 的项目,位于 C:\ProjectA。 它引用了一个名为 B.dll 的 dll,位于 C:\Binaries 中。

现在 B.dll 必须动态加载第二个名为 C.DLL 的 DLL,它位于同一文件夹 (C:\Binaries) 中。但B如何确定C的位置呢?

我知道 AppDomain.CurrentDomain.BaseDirectory 和 Assembly.GetExecutingAssembly().Location,但两者都会返回“C:\ProjectA\”,因为 B.dll 是由 A.exe 加载的。

我知道明显的解决方案是将所有二进制文件放在同一个文件夹中,并且它们将在发布时发布,但是在开发时我无法更改存储库的布局,并且我想避免对路径进行硬编码。

编辑:抱歉重复 如何获取代码所在程序集的路径?

I have project called A, located in C:\ProjectA.
It references a dll called B.dll, located in C:\Binaries.

Now B.dll has to dynamicly load a second DLL called C.DLL which is in the same folder (C:\Binaries). But how can B determine C's location?

I know about AppDomain.CurrentDomain.BaseDirectory and Assembly.GetExecutingAssembly().Location, but both will return 'C:\ProjectA\', because B.dll was loaded by A.exe.

I know the obvious solution would be to place all binaries in the same folder, and they will be when released, but while developing I cannot change the repositry's layout, and I want to avoid to hardcode the paths.

Edit: Sorry duplicate of How do I get the path of the assembly the code is in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-11-24 03:16:35

MSDN ,您必须根据关于 C(或 B)中存在的某种类型:

Assembly assembly = Assembly.GetAssembly(yourVar.GetType());
//your location will be in assembly.Location
Console.WriteLine("Location=" + assembly.Location);

From MSDN, you have to test it based on some type existing in C (or B):

Assembly assembly = Assembly.GetAssembly(yourVar.GetType());
//your location will be in assembly.Location
Console.WriteLine("Location=" + assembly.Location);
撧情箌佬 2024-11-24 03:16:35

如何使用 Assembly.GetCallingAssembly 来自 B?这将返回调用当前正在执行的方法的方法的 Assembly 对象。 (即B)

public void BMethod()
{
     var assembly = Assembly.GetCallingAssembly();
     string path = assembly.Location;
      //now use this path to load C.dll in the same folder.

}

另请参见这个类似的堆栈溢出问题

How about using Assembly.GetCallingAssembly from B? This will return The Assembly object of the method that invoked the currently executing method. (ie B)

public void BMethod()
{
     var assembly = Assembly.GetCallingAssembly();
     string path = assembly.Location;
      //now use this path to load C.dll in the same folder.

}

see also this similar stack overflow question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文