how can call .net DynamicObject from script

Oct 19, 2013 at 12:10 PM
We have a lot legacy automation scripts and need to support back compatibility,
I want to redirect ActiveXObjects to compatible with them .Net
TryInvokeMember is not called

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;
using System.Dynamic;

class ActiveXObject : DynamicObject
{
    public ActiveXObject(string exo)
    {
        var t = exo;
    }

    public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
    {
        return base.TryInvoke(binder, args, out result);
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return base.GetDynamicMemberNames();
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        return base.TryInvokeMember(binder, args, out result);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return base.TryGetMember(binder, out result);
    }

    public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
    {
        return base.TryGetIndex(binder, indexes, out result);
    }

}

class Program
{
    static void Main(string[] args)
    {

        // create a script engine
        using (var engine = new V8ScriptEngine())
        {
            // expose a host type
            engine.AddHostType("ActiveXObject", typeof(ActiveXObject));
            engine.Evaluate("var a = new ActiveXObject('object ....')");
            engine.Evaluate("a.Call(1, 3)");
        }
    }
}
}
Coordinator
Oct 19, 2013 at 1:58 PM
Hi luboid!

First, make sure you're using ClearScript 5.3.8 or newer.

Second, your object needs to declare its dynamic member list via GetDynamicMemberNames():
public override IEnumerable<string> GetDynamicMemberNames()
{
    yield return "Call";
}
Once you've added this, TryInvokeMember() will be called.

Good luck!
Oct 21, 2013 at 5:42 AM
Yes, but method GetDynamicMemberNames isn't called.
I checked it.
Coordinator
Oct 21, 2013 at 12:22 PM
Support for this form of dynamic host member invocation is new in ClearScript 5.3.8, which was released just last week. We've tested your exact code above and verified that GetDynamicMemberNames() is called, and with the change above, TryInvokeMember() is subsequently called with binder.Name set to "Call".

Please make sure you have ClearScript 5.3.8. If you have the source code, see the file Version.tt. If you only have binaries, you can see the version number in the Details property page for any of the ClearScript DLLs.
Oct 21, 2013 at 7:40 PM
Thanks,
I had used NuGet 5.3.7 now working