Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Working with dynamic .NET objects

0
0
Hi ClearScript,

I would like to work with dynamic .NET objects, i.e. the following way
    public class MyDynamicObject : DynamicObject
    {
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (binder.Name == "AnyMethod")
                // do some stuff here
                ;

            return true; // base.TryInvokeMember(binder, args, out result);
        }
    }

    // and the usage
    using (var engine = new V8ScriptEngine())
    {
        engine.AddHostObject("app", new MyDynamicObject());
                
        engine.Execute("app.AnyMethod();");
    }
I hoped that the call app.AnyMethod is landing at TryInvokeMember for doing some additional stuff here, but it doesn't. Instead I get the exception: TypeError: Object #<HostObject> has no method 'AnyMethod'. Of course, when I add AnyMethod to MyDynamicObject it works, but that's not what I want in this case. Does the calling works by reflection or/and do I have a chance to achieve the usage of dynamic objects?

Best regards,
Torsten

Viewing all articles
Browse latest Browse all 2297