C# Constructor

Sep 12, 2016 at 7:58 AM

I have two class definitions in c# like this:
public class Foo
    public Foobar(int x)
public class Bar : Foo
    public Bar(int x) : base(x)
Now I would like to make the constructors available to the JavaScript Engine. However, not as global functions but as functions of some object. Something like this:
var engine = new V8ScriptEngine( name, flags, debugPort );
engine.Script.SomeObject.Foo = engine.CompileConstructor(typeof(Foo));
engine.Script.SomeObject.Bar = engine.CompileConstructor(typeof(Bar));
Is there a way to accomplish this? Any chance that this would even work with JavaScript's instanceof operator?
Sep 12, 2016 at 3:56 PM
Hi ZoneRunner,

There are several ways to do this. One is to use ClearScript's ExtendedHostFunctions class:
var xHost = new ExtendedHostFunctions();
engine.Script.SomeObject.Foo = xHost.type(typeof(Foo));
engine.Script.SomeObject.Bar = xHost.type(typeof(Bar));
engine.Execute("bar = new SomeObject.Bar(123)");
You can't use instanceof to examine a host object's type information. If that's a requirement, consider defining native JavaScript types that wrap your host types as necessary.

Good luck!
Sep 13, 2016 at 6:53 AM
Thank you.