This project has moved and is read-only. For the latest updates, please go here.

calling .net method using params from javascript

Jan 29, 2012 at 7:20 PM

  I was wondering if there is a way to export methods from .Net to javascript that allow a variable number of arguments?  I'm trying to host some particular code that expects some of its hooks to support multiple parameters.

  Thanks.  I've just started using Jurassic, it seems good so far and the documentation is excellent.

Jan 29, 2012 at 8:43 PM

Have you tried using a params object[] argument?  This should do what you want.

Jan 30, 2012 at 12:51 AM

Thanks that did work.

Jan 30, 2012 at 4:40 AM
Edited Jan 30, 2012 at 4:40 AM

Thinking about it a bit more, you should also be able to use params string[], params int[] or whatever type you need, not just object.

Jan 30, 2012 at 5:17 AM
Edited Jan 30, 2012 at 5:29 AM

Thats true, I did end up using params string[].

Still learning, now I have an ObjectInstance with a method (marked with JSMethodAttribute) that returns another ObjectInstance.  The only problem is, I can't see to access member methods (also marked with JSMethodAttribute) on the return value.

In short, should the below work?  When I call isFile() on the result of statSync, I get exception "Jurassic.JavaScriptException : TypeError: 'fileStats.isFile' is not a function":

public class NativeFS : NativeBase  // NativeBase inherits from IbhectInstance
    [JSFunction(Name = "statSync")]
    public ObjectInstance statSync(string path)
        return new NativeFileStat(_engine, AbsolutePath(path));

public class NativeFileStat : ObjectInstance
    [JSFunction(Name = "isFile")]
    public bool isFile()
        return System.IO.File.Exists(_path);
Jan 30, 2012 at 5:52 AM

Yep, that should work fine.  Are you calling PopulateFunctions() in the constructor?  The PopulateFunctions() method is the bit that looks up the [JSFunction] attributes and hooks them up as JavaScript-callable functions.

Jan 30, 2012 at 6:12 AM

Ahh yes, I wasn't calling PopulateFunctions() from those objects.  Good call (here's the hackery:, trying to run requireJS optimization in .NET)