ArgumentException:"the path is not of a legal form" 在脚本中使用 CallerFilePath 属性时

ArgumentException : "the path is not of a legal form" when using the CallerFilePath attribute in a script

上下文

我正在尝试从我将在此处调用 ScriptRunner.exe 并且我自己编写的程序以编程方式 运行 C# 脚本(.csx 文件)(因为 csi.exe 没有输出我想要的内容)。ScriptRunner.exe 是一个简单的控制台应用程序,它最有趣的功能是具有以下行:

var state = await CSharpScript.RunAsync<int>(script, referencesAndUsings, globalArgs);

ScriptRunner.exe 效果很好!然而...

问题

我的脚本包含 以下行 :

static string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string fileName = null) { return fileName; }

尤其是 [System.Runtime.CompilerServices.CallerFilePath],我得到一个 ArgumentException : "the path is not of a legal form" ;请注意,如果我通过 #load 命令使用 C# Interactive 中的同一行,则后者不会出现 - 它正确显示了我的 .csx 文件的路径。

到目前为止调查的元素

真正让我难过的是,它 在 C# Interactive 中 有效。

问题

有谁知道为什么在我的 ScriptRunner.exe 解释时它不想工作;以及如何让它发挥作用?

var state = await CSharpScript.RunAsync<int>(script, referencesAndUsings, globalArgs);

script 是脚本本身(csx 文件的内容)。因此它没有路径。 CSharpScript 对您的 csx 文件的路径一无所知。当您从脚本中调用 GetCurrentFileName 时,没有路径信息。

您需要使用 WithFilePath(csxFilePath)

ScriptOptions 中指定一个 FilePath