Add functions and property from an Interface to a new API of the engine

Sep 7, 2015 at 9:52 PM

I have the following code with a class that expose an interface. I would like to expose my interface to javascript engine, but not using "objects".

    public interface ITest
        void foo(string s);
        //... many other methods
        int bar { get; set; }

    public class Test : ITest
        public void foo(string s)

        public int bar {get;set;}
The code I would write is this :
But NOT something like
So how can I expose all my interface as an API ?
I would like to do something like that :
                ITest t = new Test();
                engine.AddHostObject("", t);
And I would not have to "wrap" each method and property, or if you have generic code that wrap all an interface, I am interested please...

Thanks for advance for your answer
Sep 8, 2015 at 3:23 AM
Hi Sybaris,

You can use the "global members" feature to achieve the desired effect:
ITest t = new Test();
engine.AddRestrictedHostObject("test", HostItemFlags.GlobalMembers, t);
Note that using AddRestrictedHostObject() in this manner ensures that only the ITest interface members are exposed for scripting. Other members of the object remain inaccessible. Use AddHostObject() if that is not the desired behavior.

Thanks for your question!
Marked as answer by sybarisfr on 3/30/2016 at 10:57 PM
Sep 8, 2015 at 7:57 AM

Thanks, Perfect :-)