public void SerializeCore(BinaryWriter bw, Type t)
        {
            bw.Write(t.FullName);
            bw.Write((uint)t.Attributes);
            bw.Write(t.BaseType.FullName);

            // TODO custom attr for t
            var cas = t.GetCustomAttributesData();
            bw.Write(cas.Count);
            if (cas.Count > 0)
            {
                var os = new ObjectSerializer(bw.BaseStream);
                foreach (var ca in cas)
                {
                    bw.Write(ca.AttributeType.FullName);
                    var ctorArgs = ca.ConstructorArguments;
                    bw.Write(ctorArgs.Count);
                    foreach (var arg in ctorArgs)
                        os.Write(arg.Value);

                    var namedArgs = ca.NamedArguments;
                    bw.Write(namedArgs.Count);
                    foreach (var arg in namedArgs)
                    {
                        bw.Write(arg.IsField);
                        bw.Write(arg.MemberName);
                        os.Write(arg.TypedValue.Value);
                    }
                }
            }

            var interfaces = t.GetInterfaces();
            bw.Write(interfaces.Length);
            foreach (var i in interfaces)
                bw.Write(i.FullName);

            // declarations

            var fields = t.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
            bw.Write(fields.Length);
            foreach (var f in fields)
            {
                bw.Write(f.Name);
                bw.Write(f.FieldType.FullName);
                bw.Write((uint)f.Attributes);

                // TODO custom attr for f
                //f.GetCustomAttributesData();

                // TODO constant?
                bw.Write(Names.Field(f));
            }

            var methodsTodo = new List<MethodBase>();
            var methods = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Where((m) => !m.IsSpecialName);
            bw.Write(methods.Count());
            foreach (var m in methods)
            {
                Write(bw, m);
                methodsTodo.Add(m);
            }

            var props = t.GetProperties();
            bw.Write(props.Length);
            foreach (var pr in props)
            {
                Console.WriteLine(pr.Name);
                bw.Write(pr.Name);
                bw.Write((uint)pr.Attributes);
                bw.Write(pr.PropertyType.FullName);

                var ps = pr.GetIndexParameters();
                bw.Write(ps.Length);
                foreach (var p in ps)
                    bw.Write(p.ParameterType.FullName);

                // TODO custom attr for pr

                // default value
                bw.Write(false);

                bw.Write(pr.CanRead);
                if (pr.CanRead)
                {
                    Write(bw, pr.GetMethod);
                    methodsTodo.Add(pr.GetMethod);
                }
                bw.Write(pr.CanWrite);
                if (pr.CanWrite)
                {
                    Write(bw, pr.SetMethod);
                    methodsTodo.Add(pr.SetMethod);
                }
            }

            var ctors = t.GetConstructors();
            bw.Write(ctors.Length);
            foreach (var c in ctors)
            {
                Write(bw, c);
                methodsTodo.Add(c);
            }

            var ti = t.TypeInitializer;
            bw.Write(ti != null);
            if (ti != null)
            {
                Write(bw, ti, true);
                methodsTodo.Add(ti);
            }

            // definitions

            bw.Write(methodsTodo.Count);
            foreach (var m in methodsTodo)
            {
                bw.Write(Names.Method(m));
                if (!m.IsAbstract)
                    Write(bw, t.Module, m.GetMethodBody());
            }

            // others

            var nts = t.GetNestedTypes();
            bw.Write(nts.Length);
            foreach (var nt in nts)
                SerializeCore(bw, nt);
        }
            public AsyncCall(object o, string method, List<object[]> param, AsyncCallback callback, object state, TcpClient client)
            {
                Debug.Print("+ AsyncCall..ctor");

                m_state = state;
                m_completed = false;
                m_cb = callback;
                m_param = param;
                m_left = param.Count;
                m_obj = o;

                m_ns = client.GetStream();

                var tosend = new RunMessage()
                {
                    IsStatic = false,
                    MethodName = method,
                    NumArguments = param[0].Length,
                    NumCalls = param.Count,
                    TypeNames = param[0].Select(z => z.GetType().FullName).ToArray()
                };

                m_output = new ObjectDeserializer(m_ns);
                m_input = new ObjectSerializer(m_ns);
                m_input.BeginWrite(tosend, SendObject, null);
            }
 public void Cleanup()
 {
     stream.Dispose();
     input = null;
     output = null;
 }
 public void Initialize()
 {
     stream = new MemoryStream(1024);
     input = new ObjectSerializer(stream);
     output = new ObjectDeserializer(stream);
 }
        /*
        private void ProcessMessage()
        {
            var od = new ObjectDeserializer(m_bufferStream);

            var ctx = new RunnerContext(this);
            var msg = (RunMessage)od.Read();
            string staticName = string.Empty;

            if (msg.IsStatic)
                staticName = (string)od.Read();
            else
                ctx.Object = od.Read();

            for(int i = 0; i < msg.NumCalls; i++)
            {
                var args = (object[])od.Read();
                ctx.Arguments.Add(args);
            }

            //if (msg.IsStatic)

            else

            // parent run context
            Extensions.Go(() => { m_parent.Execute(ctx); });
        }*/
        internal void Respond(RunnerContext ctx)
        {
            Debug.Print("+ RemoteRunnerConnection.Respond");

            var resp = new RunResultMessage { Success = true, NumResults = ctx.Results.Count };
            var os = new ObjectSerializer(m_stream);
            os.Write(resp);
            foreach (var res in ctx.Results)
                os.Write(res);

            Debug.Print("- RemoteRunnerConnection.Respond");
        }