Search code examples
fiddlerjscriptjscript.net

Forcing specific .NET types in JScript.NET?


I am using the Fiddler web debugger which uses FiddlerScript which is based on JScript.NET. I'm trying to do some simple .NET string parsing but it fails with error "More than one method or property matches this argument list". For example:

        var ahraw: String = "one, two, three, four";
        var ah: Array = ahraw.Split([',', ' '], System.StringSplitOptions.RemoveEmptyEntries);
        for (var i in ah) {
            FiddlerObject.alert("\"" + ah[i] + "\"");
        }

enter image description here

String.Split has these prototypes:

Split(Char[], Int32, StringSplitOptions)
Split(String[], Int32, StringSplitOptions)
Split(String[], StringSplitOptions)
Split(Char[], StringSplitOptions)
Split(Char[], Int32)
Split(Char[])

Even if I add an Int32 and run it like ahraw.Split([',', ' '], 99, System.StringSplitOptions.RemoveEmptyEntries); or ahraw.Split(", ", 99, System.StringSplitOptions.RemoveEmptyEntries); it still finds multiple methods. Is it possible to force the specific .NET type? Something like var sep: String doesn't work either.

Of course there are other ways to split a string in JScript such as split() (note the lowercase s). I'm more interested in how I can get around this issue for when there's no javascript equivalent for the function I need to call, so I have to call a .NET funciton.


Solution

  • As mentioned in the comments, these days it would probably be easier to just use C# as a scripting language (could be set in Tools -> Options -> Scripting -> Language).

    If you however have/want to stick to JScript.NET (e.g. if you already have complex customizations), you can work around the problem by declaring typed array for the separators, otherwise the compiler cannot distinguish which of the Split(String[], StringSplitOptions) and Split(Char[], StringSplitOptions) overloads to call.

        var ahraw: String = "one, two, three, four";
        var separators: Char[] = [',', ' '];
        var ah: Array = ahraw.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
        for (var i in ah) {
            FiddlerObject.alert("\"" + ah[i] + "\"");
        }