0

我正在调用我Assembly.GetExecutingAssembly().Location的解决方案的其中一个 dll。当我尝试使用 Fody ( https://www.nuget.org/packages/Fody/ ) 将我的二进制文件打包成一个二进制文件时,我注意到这个调用开始返回空字符串而不是执行程序集的位置。这应该被认为是一个错误吗?使用 Fody 时有没有办法获取这些信息?如果我从解决方案的主项目进行调用,它似乎工作正常。

这是我的主要方法:

static void Main(string[] args)
{
    Console.WriteLine($"From Main: \"{Assembly.GetExecutingAssembly().Location}\"");
    Console.WriteLine($"From dll:  \"{Class1.GetLocation()}\"");
}

然后GetLocation在 dll 中定义该方法,如下所示:

public static string GetLocation()
{
    return Assembly.GetExecutingAssembly().Location;
}

没有 Fody 的控制台中的输出如下所示:

From Main: "C:\Prog\ConsoleApp1\bin\Debug\ConsoleApp1.exe"
From dll:  "C:\Prog\ConsoleApp1\bin\Debug\ClassLibrary1.dll"

添加 Fody 后,它看起来像这样:

From Main: "C:\Prog\ConsoleApp1\bin\Debug\ConsoleApp1.exe"
From dll:  ""
4

1 回答 1

2

使用Costura,默认情况下程序集是从内存中加载的,因此没有位置。

要更改此设置,您可以设置CreateTemporaryAssembliesFodyWeavers.xml其中将在加载程序集之前将它们保存到临时位置。

<Costura CreateTemporaryAssemblies='true' />
于 2021-02-04T00:59:09.510 回答