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

Sep 7, 2015 at 8:52 PM
Hi,

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)
        {
            MessageBox.Show(s);
        }

        public int bar {get;set;}
    }
The code I would write is this :
                engine.Execute("foo('hello')");
                engine.Execute("bar=6");
But NOT something like
                engine.Execute("myObject.foo('hello')");
                engine.Execute("myObject.bar=6");
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
Sybaris
Coordinator
Sep 8, 2015 at 2: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);
engine.Execute("foo('hello')");
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 6:57 AM
Hello

Thanks, Perfect :-)

Sybaris