Exemple #1
0
            public override object Invoke(__arglist)
            {
                ArgIterator ai = new ArgIterator(__arglist);

                ObjectTypeHandle[] args = new ObjectTypeHandle[ai.GetRemainingCount()];
                while (ai.GetRemainingCount() > 0)
                {
                    int    idx = args.Length - ai.GetRemainingCount();
                    object arg = TypedReference.ToObject(ai.GetNextArg());
                    args[idx] = AdapterTools.Marshal(arg);
                }
                var res = Delegate.InvokeMember("Invoke", ref args, new bool[args.Length]);

                ai = new ArgIterator(__arglist);
                while (ai.GetRemainingCount() > 0)
                {
                    int          idx = args.Length - ai.GetRemainingCount();
                    ObjectHandle arg = args[idx];
                    var          tr  = ai.GetNextArg();
                    if (__reftype(tr).IsByRef)
                    {
                        tr.SetValue(arg.Unwrap());
                    }
                }
                if (res != null)
                {
                    return(res.Unwrap());
                }
                else
                {
                    return(null);
                }
            }
Exemple #2
0
 private void RegisterMarshaller(TaskMarshaller marshaller)
 {
     Task.ContinueWith(
         t => {
         if (t.IsFaulted)
         {
             marshaller.SetException(t.Exception.InnerExceptions.ToArray());
         }
         else if (t.IsCanceled)
         {
             marshaller.SetCanceled();
         }
         else if (t.IsCompleted)
         {
             if (ResultType != null)
             {
                 marshaller.SetResult(AdapterTools.Marshal(((dynamic)t).Result));
             }
             else
             {
                 marshaller.SetResult(null);
             }
         }
     }
         );
 }
Exemple #3
0
        private bool Try(CallSiteBinder binder, object[] args, out object result)
        {
            ObjectHandle[] argHandles = AdapterTools.Marshal(args);
            var            binding    = new BindingInfo(binder);
            var            resHandle  = Adapter.InvokeMember(binding, ref argHandles);

            AdapterTools.Unmarshal(argHandles).CopyTo(args, 0);
            result = AdapterTools.Unmarshal(resHandle);
            return(true);
        }
Exemple #4
0
        private bool Try(CallSiteBinder binder, string member, object[] args, out object result)
        {
            ObjectTypeHandle[]            argHandles    = AdapterTools.Marshal(args);
            BindingInfo                   binding       = new BindingInfo(binder);
            const CSharpArgumentInfoFlags flagsRefOrOut = CSharpArgumentInfoFlags.IsRef | CSharpArgumentInfoFlags.IsOut;
            ObjectTypeHandle              resHandle;

            try{
                resHandle = Adapter.InvokeMember(member, ref argHandles, binding.ArgumentFlags.Skip(1).Select(f => (f & flagsRefOrOut) != 0).ToArray());
            }catch (MissingMethodException)
            {
                result = null;
                return(false);
            }
            AdapterTools.Unmarshal(argHandles).CopyTo(args, 0);
            result = AdapterTools.Unmarshal(resHandle);
            return(true);
        }
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage msgCall = (IMethodCallMessage)msg;
            var oArgs = msgCall.Args;
            var args  = AdapterTools.Marshal(oArgs);

            try{
                var ret = Adapter.InvokeMember(msgCall.MethodBase, ref args);
                oArgs = AdapterTools.Unmarshal(args);
                return(new ReturnMessage(AdapterTools.Unmarshal(ret), oArgs, oArgs.Length, msgCall.LogicalCallContext, msgCall));
            }catch (TargetInvocationException e)
            {
                return(new ReturnMessage(e.InnerException, msgCall));
            }catch (Exception e)
            {
                return(new ReturnMessage(e, msgCall));
            }
        }
        public ObjectHandle InvokeMember(BindingInfo binding, ref ObjectHandle[] args)
        {
            object[] oArgs = AdapterTools.Unmarshal(args);

            var binder = binding.CreateBinder();

            object[] argsObj = new object[args.Length + 1];
            argsObj[0] = Value;
            oArgs.CopyTo(argsObj, 1);

            Expression dynExpr;
            object     ret;
            const CSharpArgumentInfoFlags flagsRefOrOut = CSharpArgumentInfoFlags.IsRef | CSharpArgumentInfoFlags.IsOut;
            var argsConst = Expression.Constant(argsObj);
            var argsExp   = argsObj.Select((o, i) => (binding.ArgumentFlags[i] & flagsRefOrOut) != 0?(Expression)Expression.ArrayAccess(argsConst, Expression.Constant(i)):Expression.Constant(o));

            try{
                if ((binding.Flags & CSharpBinderFlags.ResultDiscarded) != 0)
                {
                    dynExpr = Expression.Dynamic(binder, typeof(void), argsExp);
                    Expression.Lambda <Action>(dynExpr).Compile().Invoke();
                    ret = null;
                }
                else
                {
                    dynExpr = Expression.Dynamic(binder, typeof(object), argsExp);
                    ret     = Expression.Lambda <Func <object> >(dynExpr).Compile().Invoke();
                }
            }catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }
            for (int i = 0; i < binding.ArgumentFlags.Length; i++)
            {
                if ((binding.ArgumentFlags[i] & flagsRefOrOut) == 0)
                {
                    argsObj[i] = null;
                }
            }

            Array.Copy(argsObj, 1, oArgs, 0, oArgs.Length);
            args = AdapterTools.Marshal(oArgs);
            return(AdapterTools.Marshal(ret));
        }
Exemple #7
0
        /// <summary>
        /// Invokes a method on the stored instance. An array of <see cref="ObjectTypeHandle"/> must be used to pass the arguments.
        /// </summary>
        /// <param name="method">The method reference to invoke on the instance.</param>
        /// <param name="args">The arguments to invoke the method with.</param>
        /// <returns>The return value of the method invocation.</returns>
        public ObjectTypeHandle InvokeMember(MethodBase method, ref ObjectTypeHandle[] args)
        {
            object[] oArgs = AdapterTools.Unmarshal(args);
            object   ret;

            try{
                ret = method.Invoke(Value, oArgs);
            }catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }
            var pars = method.GetParameters();

            for (int i = 0; i < pars.Length; i++)
            {
                if (!pars[i].ParameterType.IsByRef)
                {
                    oArgs[i] = null;
                }
            }
            args = AdapterTools.Marshal(oArgs);
            return(AdapterTools.Marshal(ret));
        }