脚本#:确定导入的类型是否为空

时间:2010-12-04 00:55:55

标签: script#

我正在编写调用外部JavaScript库Foo的代码,但仅限于定义了Foo。等效的JavaScript代码应如下所示:

if (typeof(Foo) != "undefined") {
  Foo.bar();
}

我在脚本#:

中尝试了以下内容
  • if(!Script.IsNullOrDefined(Foo)):我的项目需要aacorlib,不能使用sscorlib,其中定义了IsNullOrDefined。
  • if(typeof(Foo).ToString()!=“undefined”): Foo.toString()!=='undefined'中的结果将失败,因为Foo未定义。
  • if((string)Type.InvokeMethod(null,“typeof”,“Foo”)!=“undefined”):编译错误“全局方法的名称必须是有效的标识符“,指的是”typeof“。
  • if(Type.IsClass(typeof(Foo))):在运行时因“Foo undefined”错误而失败。
  • if(typeof(Foo)!= null):发出“if(Foo!= null)”,引发“Foo undefined”错误。

脚本#代码应该如何生成此JavaScript代码?

1 个答案:

答案 0 :(得分:1)

Script.Literal("if (typeof(Foo) != \"undefined\") { Foo.bar(); }");
相关问题