Host objects that implement IExpando (IDispatchEx)

Oct 1, 2015 at 3:18 PM
It looks like it is not possible to dynamically add properties from Javascript to host objects that implement IExpando interface (COM objects implementing IDispatchEx)

ClearScript is hitting the following in HostItem IExpando.AddField:
throw new NotSupportedException("Object does not support dynamic fields");
Am I missing something? If not, would this be something that will be implemented?
Coordinator
Oct 1, 2015 at 5:35 PM
Hello,

That's correct; ClearScript doesn't provide dynamic exposure for IReflect/IExpando instances.

Questions:
  • What script engine(s) are you using?
  • Are these managed objects? Or do you need to expose native COM objects?
In any case, this isn't a scenario we've encountered before. If you need to get this working quickly, consider creating a wrapper that implements ClearScript's IPropertyBag or System.Dynamic.IDynamicMetaObjectProvider (or derives from System.Dynamic.DynamicObject).

Good luck!
Oct 1, 2015 at 6:55 PM
I am using JScript engine. I am exposing IE's HTMLWindow2 object to the engine as a global object.

I suppose my best option would be to implement IDynamicMetaObjectProvider.
Coordinator
Oct 1, 2015 at 7:50 PM
Interesting. Is JavaScript code able to access the object's predefined members (e.g., alert())?
Oct 1, 2015 at 8:58 PM
Yes, accessing members works fine.
Oct 2, 2015 at 3:48 AM
I ended up modifying ClearScript source to solve this problem.

I modified HostItem
private bool BindSpecialTarget<T>(out T specialTarget) where T : class
and included the following code at the beginning of the method:
            if (typeof(T) == typeof(IDynamic))
            {
                if (target.Type.IsCOMObject)
                {
                    T dyn = this.engine.MarshalToHost(target.Target, false) as T;
                    if (dyn != null)
                    {
                        specialTarget = dyn;
                        return true;
                    }
                }
            }
Coordinator
Oct 5, 2015 at 5:08 PM
Great to hear that you found a solution. Thanks!
Coordinator
Oct 20, 2015 at 5:44 PM
We're tracking this issue here; it'll be fixed in the next point release. Thanks again!