5

I figured out I cannot load one script library from another easily:

module.csx

string SomeFunction() {
   return "something";
}

script.csx

ExecuteFile("module.csx");
SomeFunction() <-- causes compile error "SomeFunction" does not exist

This is because the compiler does not know of module.csx at the time it compiles script.csx afaiu. I can add another script to load the two files from that one, and that will work. However thats not that pretty.

Instead I like to make my scripthost check for a special syntax "load module" within my scripts, and execute those modules before actual script execution.

script.csx

// load "module.csx"
SomeFunction()

Now, with some basic string handling, I can figure out which modules to load (lines that contains // load ...) and load that files (gist here https://gist.github.com/4147064):

foreach(var module in scriptModules) {
   session.ExecuteFile(module);
}
return session.Execute(script)

But - since we're talking Roslyn, there should be some nice way to parse the script for the syntax I'm looking for, right?

And it might even exist a way to handle module libraries of code?

4

3 回答 3

6

目前在 Roslyn 中没有办法引用另一个脚本文件。我们正在考虑#load从交互式窗口的宿主命令转变为语言的一部分(如#r),但目前尚未实现。

至于如何处理字符串,你可以正常解析它,然后寻找未知类型的预处理器指令并深入研究结构。

于 2012-11-28T15:51:55.440 回答
6

自https://github.com/dotnet/roslyn/commit/f1702c#load起已添加对 in 脚本文件的支持。

此功能将在 Visual Studio 2015 Update 1 中提供。

于 2015-11-19T20:01:23.687 回答
1

包括脚本:

#load "common.csx"
...

并在运行脚本时配置源解析器:

Script<object> script = CSharpScript.Create(code, ...);
var options = ScriptOptions.Default.WithSourceResolver(new SourceFileResolver(new string[] { }, baseDirectory));
var func = script.WithOptions(options).CreateDelegate()
...
于 2017-11-21T19:44:40.670 回答