Example #1
0
        public override object GetProperty(string name, object[] args)
        {
            VerifyNotDisposed();

            var result = engine.MarshalToHost(engine.ScriptInvoke(() =>
            {
                try
                {
                    return(target.InvokeMember(name, BindingFlags.GetProperty, null, target, engine.MarshalToScript(args), null, CultureInfo.InvariantCulture, null));
                }
                catch (Exception)
                {
                    if (target.GetMethod(name, BindingFlags.GetProperty) != null)
                    {
                        // Property retrieval failed, but a method with the given name exists;
                        // create a tear-off method. This currently applies only to VBScript.

                        return(new ScriptMethod(this, name));
                    }

                    return(Nonexistent.Value);
                }
            }), false);

            var resultScriptItem = result as WindowsScriptItem;

            if ((resultScriptItem != null) && (resultScriptItem.engine == engine))
            {
                resultScriptItem.holder = this;
            }

            return(result);
        }
    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;
            }
        }
    }