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

New Post: Dynamic properties added to DynamicObject are undefined

0
0
Hi,

First, thanks again for an amazing project.

It seems that dynamic properties added to a DynamicObject are not handled correctly.
    class MyDynamicObject : DynamicObject
    {
        private Dictionary<string, object> _dict;
        public MyDynamicObject()
            : base()
        {
            this._dict = new Dictionary<string, object>();
        }

        public string RealProp { get; set; }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return this._dict.TryGetValue(binder.Name, out result);
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this._dict[binder.Name] = value;
            return true;
        }
    }


    class DynamicTest
    {
        public void Run()
        {
            using (var engine = new V8ScriptEngine())
            {
                dynamic myDynamicObject = new MyDynamicObject();

                string val = "Value1";
                myDynamicObject.DynProp = val;
                myDynamicObject.RealProp = val;
                
                engine.AddHostObject("myDynamicObject", myDynamicObject);
                engine.AddHostType("Console", typeof(Console));

                Console.WriteLine("myDynamicObject.DynProp = " + myDynamicObject.DynProp);

                Console.WriteLine("myDynamicObject.RealProp = " + myDynamicObject.RealProp);

                // The following print 'undefined'
                engine.Execute("Console.WriteLine('In script: myDynamicObject.DynProp = ' + myDynamicObject.DynProp)");

                engine.Execute("Console.WriteLine('In script: myDynamicObject.RealProp = ' + myDynamicObject.RealProp)");

                // Dynamic properties added to ExpandoObject are OK
                dynamic myExpandoObject = new ExpandoObject();
                myExpandoObject.DynProp = val;
                engine.AddHostObject("myExpandoObject", myExpandoObject);
                engine.Execute("Console.WriteLine('In script: myExpandoObject.DynProp = ' + myExpandoObject.DynProp)");
            }
        }
    }
The output of this test is:
myDynamicObject.DynProp = Value1
myDynamicObject.RealProp = Value1
In script: myDynamicObject.DynProp = undefined
In script: myDynamicObject.RealProp = Value1
In script: myExpandoObject.DynProp = Value1
Note that the same test for ExpandoObject works OK.
It seems to me that ClearScript overrides TryGetMember() without calling base.TryGetMember().

Thanks again,
Ron

Viewing all articles
Browse latest Browse all 2297