static ReturnInfo ExecuteField(IAsyncInvoke async, VarPool varManager, ProtocolInfo info,
                                       object obj, object[] args, FieldInfo field)
        {
            if (args.Length == 0)
            {
                VarAddress getVar = varManager.Add(null);

                KeepAlive(varManager, info.Arguments, getVar);

                async.Execute(delegate
                {
                    ReturnInfo retInfo = new ReturnInfo();
                    try
                    {
                        varManager.SetObject(getVar, field.GetValue(obj));
                    }
                    catch (Exception e)
                    {
                        retInfo = new ReturnInfo(new ExceptionInfo(e));
                    }

                    varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                    FreeKeepAlive(varManager, info.Arguments, getVar);
                });
                return(new ReturnInfo(getVar));
            }
            else if (args.Length == 1)
            {
                KeepAlive(varManager, info.Arguments, null);

                async.Execute(delegate
                {
                    ReturnInfo retInfo = new ReturnInfo();
                    try
                    {
                        field.SetValue(obj, args[0]);
                    }
                    catch (Exception e)
                    {
                        retInfo = new ReturnInfo(new ExceptionInfo(e));
                    }

                    varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                    FreeKeepAlive(varManager, info.Arguments, null);
                });
                return(new ReturnInfo());
            }
            throw new InternalException();
        }
        static ReturnInfo SetValue(VarPool varManager, ProtocolInfo info)
        {
            if (info.Arguments.Length != 1)
            {
                throw new InternalException();
            }

            object[] args;
            ResolveArgs(varManager, info.Arguments, out args);

            varManager.SetObject(info.VarAddress, args[0]);
            return(new ReturnInfo());
        }
        static ReturnInfo ExecuteMethodOrProperty(IAsyncInvoke async, VarPool varManager,
                                                  ProtocolInfo info, object obj, object[] args, MethodInfo method)
        {
            VarAddress handle = null;

            if (method.ReturnParameter.ParameterType != typeof(void))
            {
                handle = varManager.Add(null);
            }

            KeepAlive(varManager, info.Arguments, handle);

            async.Execute(delegate
            {
                ReturnInfo retInfo = new ReturnInfo();
                try
                {
                    object retObj = method.Invoke(obj, args);
                    if (method.ReturnParameter.ParameterType != typeof(void))
                    {
                        varManager.SetObject(handle, retObj);
                    }
                    List <object> retArgsTmp = new List <object>();
                    retArgsTmp.Add(null);
                    retArgsTmp.AddRange(args);
                    ReflectArgsAfterInvoke(varManager, info.Arguments, retArgsTmp.ToArray());
                }
                catch (Exception e)
                {
                    retInfo = new ReturnInfo(new ExceptionInfo(e));
                }
                varManager.SetObject((VarAddress)info.Arguments[0], retInfo);

                FreeKeepAlive(varManager, info.Arguments, handle);
            });

            return(new ReturnInfo(handle));
        }
 internal static void ReflectArgsAfterInvoke(VarPool varManager, object[] argsInfo, object[] args)
 {
     if (argsInfo.Length != args.Length)
     {
         throw new InternalException();
     }
     for (int i = 0; i < argsInfo.Length; i++)
     {
         VarAddress handle = argsInfo[i] as VarAddress;
         if (handle != null)
         {
             varManager.SetObject(handle, args[i]);
         }
     }
 }