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 文件的路径。
到目前为止调查的元素
- 堆栈跟踪显示
at System.IO.Path.LegacyNormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
- 我检查了一下似乎是 implementation
- 我手动检查了我的 csx 文件的路径;我的 csx 路径中没有无效路径字符,也没有通配符。
- 我检查过
mscorlib
没有引用问题
可能ScriptOptions(我的第一个示例代码中的referencesAndUsings
)中缺少某些东西,我看了看但是......我似乎并没有完全理解够好了
- 我创建
ScriptOptions
("referencesAndUsings
") 的方式如下所示:
var myOptions = ScriptOptions.Default;
myOptions.AddReferences(new List<string>() { ... });
myOptions.AddImports(new List<string>() { ... });
This 是 CallerFilePath
属性的文档
- This 是呼叫者信息概念的文档
真正让我难过的是,它 在 C# Interactive 中 有效。
问题
有谁知道为什么在我的 ScriptRunner.exe
解释时它不想工作;以及如何让它发挥作用?
在
var state = await CSharpScript.RunAsync<int>(script, referencesAndUsings, globalArgs);
script
是脚本本身(csx 文件的内容)。因此它没有路径。 CSharpScript 对您的 csx 文件的路径一无所知。当您从脚本中调用 GetCurrentFileName
时,没有路径信息。
您需要使用 WithFilePath(csxFilePath)
在 ScriptOptions
中指定一个 FilePath
上下文
我正在尝试从我将在此处调用 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 文件的路径。
到目前为止调查的元素
- 堆栈跟踪显示
at System.IO.Path.LegacyNormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
- 我检查了一下似乎是 implementation
- 我手动检查了我的 csx 文件的路径;我的 csx 路径中没有无效路径字符,也没有通配符。
- 我检查了一下似乎是 implementation
- 我检查过
mscorlib
没有引用问题
可能ScriptOptions(我的第一个示例代码中的
referencesAndUsings
)中缺少某些东西,我看了看但是......我似乎并没有完全理解够好了- 我创建
ScriptOptions
("referencesAndUsings
") 的方式如下所示:
var myOptions = ScriptOptions.Default; myOptions.AddReferences(new List<string>() { ... }); myOptions.AddImports(new List<string>() { ... });
- 我创建
This 是
CallerFilePath
属性的文档- This 是呼叫者信息概念的文档
真正让我难过的是,它 在 C# Interactive 中 有效。
问题
有谁知道为什么在我的 ScriptRunner.exe
解释时它不想工作;以及如何让它发挥作用?
在
var state = await CSharpScript.RunAsync<int>(script, referencesAndUsings, globalArgs);
script
是脚本本身(csx 文件的内容)。因此它没有路径。 CSharpScript 对您的 csx 文件的路径一无所知。当您从脚本中调用 GetCurrentFileName
时,没有路径信息。
您需要使用 WithFilePath(csxFilePath)
ScriptOptions
中指定一个 FilePath