This project has moved. For the latest updates, please go here.

Working with dynamic .NET objects

Apr 6, 2014 at 3:15 PM
Edited Apr 6, 2014 at 3:22 PM
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());
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,
Apr 7, 2014 at 3:13 AM
Hello Torsten,

For this to work, your class must implement DynamicObject.GetDynamicMemberNames(). For example:
public override IEnumerable<string> GetDynamicMemberNames()
    yield return "AnyMethod";
Good luck!
Apr 7, 2014 at 6:38 PM
That works. Thanks a lot!