【发布时间】:2013-09-26 08:48:19
【问题描述】:
我尝试使用ClearScript 加载TypeScript 编译器以编译一些基本的TypeScript 代码。
不幸的是,在执行 TypeScript 编译器源时,我收到了这个错误:
'WScript' 未定义
这是我使用的 LINQPad 程序,将 ClearScript dll 和 TypeScript compiler file 放在 .linq 程序旁边:
void Main()
{
using (var js = new Microsoft.ClearScript.Windows.JScriptEngine(Microsoft.ClearScript.Windows.WindowsScriptEngineFlags.DisableSourceManagement))
{
var typeScriptSource = File.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), "tsc.js"));
js.Execute(typeScriptSource);
const string typeScriptCode = @"
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return ""Hello, "" + this.greeting;
}
}
function test()
{
var greeter = Greeter(""world"");
return greeter.greet();
}
";
js.Script.TypeScript.Compile(typeScriptCode);
object result = js.Script.test();
result.Dump();
}
}
#region Copy ClearScript to correct location
static UserQuery()
{
foreach (var filename in new[] { "ClearScriptV8-32.dll", "ClearScriptV8-64.dll", "v8-ia32.dll", "v8-x64.dll" })
{
try
{
string sourcePath = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), filename);
string targetPath = Path.Combine(Path.GetDirectoryName(typeof(Util).Assembly.Location), filename);
File.Copy(sourcePath, targetPath, true);
}
catch (IOException ex)
{
unchecked
{
const int fileInUseHresult = (int)0x80070020;
if (ex.HResult != fileInUseHresult)
throw;
}
}
}
}
#endregion
错误发生在这一行:
js.Execute(typeScriptSource);
我已经创建了一个包含所有内容的 .zip 文件,您需要 LINQPad 来加载 .linq 文件并进行实验。 ClearScript dll 是从未经修改的源创建的,但如果您不相信我,您应该能够自己复制它们(如果您没有它们的话)。
可在此处获得:Dropbox Link to SO19023498.zip。
我尝试过的:
-
我尝试先执行这段代码:
var WScript = new ActiveXObject("WSH.WScript");这只产生了这个错误:
Automation server can't create object我没有在 HKEY_CLASSES_ROOT 下的注册表中看到
WSH.WScript,所以可能是这样。 我尝试弄清楚如何从 .NET 创建对象并将其设置到脚本上下文中,但我显然没有找到正确的位置。
【问题讨论】:
-
请注意,我不知道编译打字稿代码的调用是否正确,也许它会返回生成的javascript代码,但程序在该步骤之前失败。
-
WScript 对象不可创建,尽管您可以使用 C# 类相当容易地模拟它。 tsc.js 脚本还需要 ActiveXObject,它是一种 JScript 主义,但如果您想使用 V8,也可以模拟出来(我假设您这样做,因为您正在费力安装 ClearScript 的 V8 DLL)。不过,这一切都需要一些工作,所以我会在下面推荐 Ela 的方法。
标签: c# .net typescript windows-scripting clearscript