Calling .NET assemblies from in Javascript .NET

Jan 5, 2011 at 5:40 PM

Can you create an instance of a .NET object?  


var myColor = System.Drawing.Color.Red;

You can create and call object in Jint this way.  I didn't know if you had a similar method.

Jan 5, 2011 at 5:58 PM

You can easily create some code to support this functionnality:

    public class SystemInvoker
        public object Create(string iTypeName, object[] iArgs)
            Type type = Type.GetType(iTypeName);

            if (type == null && !mTypes.TryGetValue(iTypeName, out type))
                throw new Exception("Unknown type: " + iTypeName);
            return Activator.CreateInstance(type, iArgs);

You must then expose a SystemInvoker instance in your context:


    context.SetParameter("System", new SystemInvoker());


You can then create instances of any class that has been properly loaded in the Application Domain from the Javascript code:


    var myColor = System.Create("System.Drawing.Color.Red");


You will probably need to improve this code if you would like to load types from any assembly. Consider using fully qualified type names, for instance, to instantiate your classes.


Jan 11, 2011 at 5:33 PM

Javascript has import reserved as a keyword.  Why not use it?  Microsoft's used this to do things the easy way:

import System.Drawing;

voila!  You know can say:

var myColor = Color.Red;

The whole point is to make things easy for the user.  The above is as easy as it gets.  Even having to use a fully qualified name like Jint does is silly.  No one wants to type all that mess out every time a .Net type is used. 

Nov 10, 2012 at 8:49 PM


I would also appreciate the direct usage of .NET types, if possible, although the above solution is also nice. Any chance of this feature getting into the core?