开发者

Script#: Determine if imported type is null

I'm writing code that calls an external JavaScript library Foo but only if Foo is defined. The equivalent JavaScript code should looks something like this:

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

I've tried the following in Script#:

  • if (!Script.IsNullOrDefined(Foo)): My project requires aacorlib and cannot use sscorlib, where IsNullOrDefined is defined.
  • if (typeof(Foo).ToString() != "undefined"): Results in Foo.toString() !== 'undefined' which will fail because Foo is undefined.
  • if ((string)Type.InvokeMethod(null, "typeof", "Foo") != "undefined"): Compile error "The name of a global method must be a valid identifier", referring to "typeof".
  • if (Type.IsClass(typeof(Foo))):Fails at runtime with "Foo undefined" error.
  • if (typeof(Foo) != null):Emits "if(Foo!= null)" wh开发者_如何学编程ich throws "Foo undefined" error.

What should the Script# code look like to generate this JavaScript code?


Script.Literal("if (typeof(Foo) != \"undefined\") { Foo.bar(); }");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜