public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
 {
     if (indexes.Length == 1)
     {
         var memberName = indexes[0].ToString();
         value = DynamicNode.Unwrap(value);
         ReflectionHelpers.SetValue(this.value, memberName, value);
         return(true);
     }
     return(base.TrySetIndex(binder, indexes, value));
 }
    public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
    {
        IExpando self = this.value;

        object[] constructorArgs = new object[0];
        if (args.Length > 0)
        {
            self            = (IExpando)DynamicNode.Unwrap(args[0]);
            constructorArgs = new object[args.Length - 1];
            Array.ConstrainedCopy(args, 1, constructorArgs, 0, constructorArgs.Length);
        }
        result = ReflectionHelpers.Call(this.value, self, constructorArgs);
        result = DynamicNode.Wrap(result);
        return(true);
    }
    private static object Invoke(IExpando instance, string functionName, InvokeFlags flags, object[] args)
    {
        try
        {
            args = args.Select(arg => DynamicNode.Unwrap(arg)).ToArray();
            switch (flags)
            {
            case InvokeFlags.DISPATCH_METHOD:
                var method = instance.GetMethod(functionName, DefaultFlags);
                return(method.Invoke(instance, args));

            case InvokeFlags.DISPATCH_PROPERTYGET:
                var getProp = instance.GetProperty(functionName, DefaultFlags);
                return(getProp.GetValue(instance, null));

            case InvokeFlags.DISPATCH_PROPERTYPUT:
            case InvokeFlags.DISPATCH_PROPERTYPUTREF:
                var setProp = instance.GetProperty(functionName, DefaultFlags);
                if (setProp == null)
                {
                    setProp = instance.AddProperty(functionName);
                }
                setProp.SetValue(instance, args[0], null);
                return(null);

            default:
                throw new NotSupportedException();
            }
        }
        catch (COMException comex)
        {
            switch ((uint)comex.ErrorCode)
            {
            // Unexpected script error. This will be handled by the IProcess.UnhandledException event
            case 0x80020101:
                return(null);

            default:
                throw;
            }
        }
    }