开发者

Script#: Determine if imported type is null

开发者 https://www.devze.com 2023-01-28 23:55 出处:网络
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:

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

精彩评论

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

关注公众号