static void TestReadWriteVariantStruct()
        {
            using (Udbus.Core.MockStoreUdbusTransport transport = new Udbus.Core.MockStoreUdbusTransport())
            {
                //Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(transport);
                Udbus.Serialization.UdbusConnector connector = new Udbus.Serialization.UdbusConnector(transport);
                Udbus.Serialization.UdbusMessageBuilderTracker builder = new Udbus.Serialization.UdbusMessageBuilderTracker();

                Udbus.Core.IUdbusMessageVisitor visitor = new Udbus.Core.UdbusMessageVisitorDumpXen();
                Udbus.Core.IUdbusMessageVisitor visitorTrack = new Udbus.Core.UdbusMessageVisitorFind(builder);
                Udbus.Core.IUdbusMessageVisitor visitors = new Udbus.Core.UdbusMessageVisitorMulti(visitor, visitorTrack);

                int result;
                using (var msg = builder.UdbusMethodMessage(
                                    "org.freedesktop.DBus", "/org/freedesktop/DBus",
                                    "org.freedesktop.DBus", "VariantStructMessage").Message)
                {
                    Udbus.Types.dbus_sig signature = Udbus.Types.dbus_sig.Initialiser;
                    signature.a[0] = Udbus.Types.dbus_type.DBUS_SIGNATURE;
                    signature.a[1] = Udbus.Types.dbus_type.DBUS_INVALID;
                    // As it happens these struct fields will double as dictionary entries...
                    object[] structFields = new object[] { 1, new object[] {"one", true } };

                    Udbus.Containers.dbus_union variantStruct = Udbus.Containers.dbus_union.CreateStruct(structFields,
                        new Udbus.Types.dbus_type[] {
                            Udbus.Types.dbus_type.DBUS_STRUCT_BEGIN,
                            Udbus.Types.dbus_type.DBUS_INT32,
                            Udbus.Types.dbus_type.DBUS_STRUCT_BEGIN,
                            Udbus.Types.dbus_type.DBUS_STRING,
                            Udbus.Types.dbus_type.DBUS_BOOLEAN,
                            Udbus.Types.dbus_type.DBUS_STRUCT_END,
                            Udbus.Types.dbus_type.DBUS_STRUCT_END,
                            Udbus.Types.dbus_type.DBUS_INVALID
                        }
                    );

                    builder.SetSignature(ref signature)
                        .BodyAdd(4096);
                    builder
                        .BodyAdd_Variant(variantStruct)
                    ;
                    result = connector.Send(msg);
                    Console.WriteLine("VariantStructMessage send result: {0}", result);
                }

                using (var msg = connector.ReceiveHandle(out result))
                {
                    Console.WriteLine("VariantStructMessage recv result: {0}. msg: {1}", result, msg);
                    if (result == 0) // If got message ok
                    {
                        Udbus.Core.UdbusMessagePair messageData = new Udbus.Core.UdbusMessagePair(msg);
                        Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(msg);

                        Udbus.Core.UdbusVisitorFunctions.Visit(result, msg, visitor);
                        Udbus.Containers.dbus_union variantReadStruct = reader.ReadVariant();
                        Console.WriteLine(string.Format("variantReadStruct: {0}", variantReadStruct.ToString()));

                    } // Ends if message ok
                } // Ends using receive message
            } // Ends using transport
        }
        static Udbus.Serialization.UdbusConnector TestDbusGetProperty(Udbus.Serialization.IUdbusTransport connection, PreNextActionDelegate preNextAction)
        {
            System.Threading.ManualResetEvent stop = new System.Threading.ManualResetEvent(false);
            Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(connection);
            Udbus.Serialization.UdbusMessageBuilderTracker builder = new Udbus.Serialization.UdbusMessageBuilderTracker();

            // Visitors
            Udbus.Core.IUdbusMessageVisitor visitor = new Udbus.Core.UdbusMessageVisitorDumpXen();
            Udbus.Core.IUdbusMessageVisitor visitorTrack = new Udbus.Core.UdbusMessageVisitorFind(builder);
            Udbus.Core.IUdbusMessageVisitor visitors = new Udbus.Core.UdbusMessageVisitorMulti(visitor, visitorTrack);

            TestDbusHelloImpl(builder, visitors, connector, stop, preNextAction);

            // Get property.
            TestDbusGetPropertyImpl(builder, visitors, connector, stop, preNextAction);

            return connector;
        }
        static void TestReadWriteString()
        {
            using (Udbus.Core.MockStoreUdbusTransport transport = new Udbus.Core.MockStoreUdbusTransport())
            {
                //Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(transport);
                Udbus.Serialization.UdbusConnector connector = new Udbus.Serialization.UdbusConnector(transport);
                Udbus.Serialization.UdbusMessageBuilderTracker builder = new Udbus.Serialization.UdbusMessageBuilderTracker();

                Udbus.Core.IUdbusMessageVisitor visitor = new Udbus.Core.UdbusMessageVisitorDumpXen();
                Udbus.Core.IUdbusMessageVisitor visitorTrack = new Udbus.Core.UdbusMessageVisitorFind(builder);
                Udbus.Core.IUdbusMessageVisitor visitors = new Udbus.Core.UdbusMessageVisitorMulti(visitor, visitorTrack);

                int result;
                using (var msg = builder.UdbusMethodMessage(
                                    "org.freedesktop.DBus", "/org/freedesktop/DBus",
                                    "org.freedesktop.DBus", "NotARealMessage").Message)
                {
                    Udbus.Types.dbus_sig signature = Udbus.Types.dbus_sig.Initialiser;
                    signature.a[0] = Udbus.Types.dbus_type.DBUS_STRING;
                    signature.a[1] = Udbus.Types.dbus_type.DBUS_INVALID;
                    builder.SetSignature(ref signature)
                        .BodyAdd(4096)
                        .BodyAdd_String("param1")
                    ;
                    result = connector.Send(msg);
                    Console.WriteLine("NotARealMessagesend result: {0}", result);
                }

                using (var msg = connector.ReceiveHandle(out result))
                {
                    Console.WriteLine("NotARealMessage recv result: {0}. msg: {1}", result, msg);
                    if (result == 0) // If got message ok
                    {
                        Udbus.Core.UdbusMessagePair messageData = new Udbus.Core.UdbusMessagePair(msg);
                        Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(msg);

                        Udbus.Core.UdbusVisitorFunctions.Visit(result, msg, visitor);
                        //Udbus.Core.UdbusVisitorFunctions.Visit(result, msg, visitor);
                        string param1 = reader.ReadString();
                        Console.WriteLine(string.Format("Param1: {0}", param1));

                    } // Ends if message ok
                } // Ends using receive message
            } // Ends using transport
        }
        static Udbus.Serialization.UdbusConnector TestDbusCalls(Udbus.Serialization.IUdbusTransport connection, PreNextActionDelegate preNextAction)
        {
            //int result = -1;
            System.Threading.ManualResetEvent stop = new System.Threading.ManualResetEvent(false);
            //Udbus.v4v.v4vConnection connection = new Udbus.v4v.v4vConnection(Console_io_debug);
            //Udbus.Serialization.ManagedDbusIo dbio;
            //connection.PopulateDbio(ref dbio);

            //Udbus.Core.UdbusFunctions.dbus_auth(ref dbio, "Dude");
            Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(connection);

            Udbus.Serialization.UdbusMessageBuilderTracker builder = new Udbus.Serialization.UdbusMessageBuilderTracker();

            // Visitors
            Udbus.Core.IUdbusMessageVisitor visitor = new Udbus.Core.UdbusMessageVisitorDumpXen();
            Udbus.Core.IUdbusMessageVisitor visitorTrack = new Udbus.Core.UdbusMessageVisitorFind(builder);
            Udbus.Core.IUdbusMessageVisitor visitors = new Udbus.Core.UdbusMessageVisitorMulti(visitor, visitorTrack);

            // Udbus.Serialization.NMessageStruct.UdbusMessageHandle recv;

            //preNextAction("Hello");

            //// Hello.
            //using (var msg = builder.UdbusMethodMessage(
            //                    "org.freedesktop.DBus", "/org/freedesktop/DBus",
            //                    "org.freedesktop.DBus", "Hello").Message)
            //{
            //    result = connector.Send(msg);
            //    Console.WriteLine("Hello send result: {0}", result);
            //}
            ////var recv = connector.Receive(out result);
            //result = connector.ReceiveStruct(out recv);
            //Console.WriteLine("Hello recv result: {0}. msg: {1}", result, recv);
            TestDbusHelloImpl(builder, visitors, connector, stop, preNextAction);

            // Get property.
            //TestDbusGetPropertyImpl(builder, visitors, connector, stop, preNextAction);

            #if _LISTVMS
            preNextAction("List VMs");

            using (var msg = builder.UdbusMethodMessage(
                "com.citrix.xenclient.xenmgr", "/",
                "com.citrix.xenclient.xenmgr",
                "list_vms").Message)
            {
                result = connector.Send(msg);
                Console.WriteLine("list_vms send result: {0}", result);
            }
            using (var msg = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitors, Console.Out, stop).Handle)
            {
                msg.HandleToStructure(out recv);
                Console.WriteLine("list_vms recv result: {0}. msg: {1}", result, recv);

                Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(msg);
                uint counter = 0;

                foreach (Udbus.Serialization.UdbusMessageReader subreader in reader.ArrayReader(Udbus.Types.dbus_type.DBUS_OBJECTPATH))
                {
                    Udbus.Types.UdbusObjectPath op;
                    result = subreader.ReadObjectPath(out op);
                    if (result != 0)
                    {
                        Console.WriteLine("Error ! {0:d}/0x{0:x8}", result);
                    }
                    Console.WriteLine("Entry {0:d2}: {1}", counter++, op);
                }
            } // Ends using ListNames response

            #endif // _LISTVMS

            // These days GetAllProperties tends to be blocked by firewall
            #if _GETALLPROPERTIES
            preNextAction("GetAllProperties");
            using (var msg = builder.UdbusMethodMessage(
                "com.citrix.xenclient.xenmgr", "/",
                "org.freedesktop.DBus.Properties", "GetAll").Message)
            {
                Udbus.Types.dbus_sig signature = Udbus.Types.dbus_sig.Initialiser;
                signature.a[0] = Udbus.Types.dbus_type.DBUS_STRING;
                signature.a[1] = Udbus.Types.dbus_type.DBUS_INVALID;
                builder.SetSignature(ref signature)
                    .BodyAdd(4096)
                    .BodyAdd_String("com.citrix.xenclient.xenmgr.config")
                ;
                result = connector.Send(msg);
                Console.WriteLine("GetAllProperties send result: {0}", result);
            }
            try
            {
                Udbus.Core.UdbusMessagePair recvData = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitors, Console.Out, stop);
                recv = recvData.Data;
                if (recv.typefield.type != Udbus.Types.dbus_msg_type.DBUS_TYPE_ERROR)
                {
                    Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(recvData.Handle);
                    do
                    {
            #if !READASVARIANT
                        Udbus.Parsing.BuildContext context = new Udbus.Parsing.BuildContext(new Udbus.Parsing.CodeTypeNoOpHolder());
                        Udbus.Serialization.Variant.UdbusVariantIn variantIn = new Udbus.Serialization.Variant.UdbusVariantIn();
                        Udbus.Parsing.IDLArgumentTypeNameBuilderBase nameBuilder = new Udbus.Parsing.IDLArgumentTypeNameBuilderNoOp();
                        // It's a dictionary of strings -> variants.
                        //IDictionary<string, Udbus.Containers.dbus_union> properties;
                        IEnumerable<KeyValuePair<string, Udbus.Containers.dbus_union>> properties;
                        reader.MarshalDict(Udbus.Serialization.UdbusMessageReader.ReadString,
                            Udbus.Serialization.UdbusMessageReader.ReadVariant,
                            out properties);
                        //Udbus.Types.dbus_sig sig = Udbus.Types.dbus_sig.Initialiser;
                        //result = reader.ReadSignature(ref sig);
                        //Udbus.Parsing.CodeBuilderHelper.BuildCodeParamType(variantIn, nameBuilder, sig.a, context);
                        foreach (KeyValuePair<string, Udbus.Containers.dbus_union> entry in properties)
                        {
                            Console.WriteLine("{0} => {1}", entry.Key, entry.Value.ToString());
                        }

            #else // !READASVARIANT
                        Udbus.Types.dbus_sig sig = Udbus.Types.dbus_sig.Initialiser;
                        result = reader.ReadSignature(ref sig);
                        if (result != 0)
                        {
                            Console.WriteLine("Error reading property variant signature: {0}", result);
                            break;
                        }
                        string isopath;
                        result = reader.ReadString(out isopath);
                        if (result != 0)
                        {
                            Console.WriteLine("Error reading property variant string: {0}", result);
                            break;
                        }

                        Console.WriteLine("iso_path: {0}", isopath);
            #endif // READASVARIANT
                    } while (false);
                }
                else
                {
                    Console.WriteLine("Getting the property failed. Boooooo ");
                }
            }
            catch (System.Runtime.InteropServices.SEHException seh)
            {
                Console.WriteLine("Error: " + seh.ToString());
            }
            #endif //_GETALLPROPERTIES

            #if _LISTNAMES
            preNextAction("ListNames");

            // List Names.
            using (var msg = builder.UdbusMethodMessage(
                "org.freedesktop.DBus", "/org/freedesktop/DBus",
                "org.freedesktop.DBus", "ListNames").Message)
            {
                result = connector.Send(msg);
                Console.WriteLine("ListNames send result: {0}", result);
            }

            //result = connector.ReceiveStruct(out recv);
            //Console.WriteLine("ListNames recv result: {0}. msg: {1}", result, recv);

            //using (var msg = connector.ReceiveHandle(out result))
            using(var msg = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitors, Console.Out, stop).Handle)
            {
                //result = connector.ReceiveStruct(out recv);
                msg.HandleToStructure(out recv);
                Console.WriteLine("ListNames recv result: {0}. msg: {1}", result, recv);

                Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(msg);
                uint counter = 0;

                foreach (Udbus.Serialization.UdbusMessageReader subreader in reader.ArrayReader(Udbus.Types.dbus_type.DBUS_STRING))
                {
                    string name = subreader.ReadStringValue(out result);
                    if (result != 0)
                    {
                        Console.WriteLine("Error ! {0:d}/0x{0:x8}", result);
                    }
                    Console.WriteLine("Entry {0:d2}: {1}", counter++, name);
                }
            } // Ends using ListNames response
            #endif // _LISTNAMES

            // read_icon
            TestDbusReadIcon(builder, visitors, connector, stop, preNextAction);

            #if _GETSIGNAL
            preNextAction("AddMatch");

            // AddMatch for signal.
            using (var msg = builder.UdbusMethodMessage(
                "org.freedesktop.DBus", "/org/freedesktop/DBus",
                "org.freedesktop.DBus", "AddMatch").Message)
            {
                Udbus.Types.dbus_sig signature = Udbus.Types.dbus_sig.Initialiser;
                signature.a[0] = Udbus.Types.dbus_type.DBUS_STRING;
                signature.a[1] = Udbus.Types.dbus_type.DBUS_INVALID;
                builder.SetSignature(ref signature)
                    .BodyAdd(4096)
                    .BodyAdd_String("type='signal',interface='com.citrix.xenclient.xenmgr.host',member='storage_space_low'")
                    //.BodyAdd_String("type='signal',interface='com.citrix.xenclient.xenmgr.host'")
                ;
                result = connector.Send(msg);
                Console.WriteLine("AddMatch send result: {0}", result);

            }

            preNextAction("LoopSignals");

            // Handle signals and other bits of magic.
            //Console.TreatControlCAsInput = true;
            Console.CancelKeyPress += delegate(Object sender, ConsoleCancelEventArgs consoleargs)
            {
                consoleargs.Cancel = true;
                Console.WriteLine("Setting stop event...");
                stop.Set();
                connection.Cancel();
            };

            try
            {
                Udbus.Core.UdbusVisitorFunctions.LoopUdbus(connector, visitor, Console.Out, stop);
            }
            catch (System.Runtime.InteropServices.SEHException seh)
            {
                Console.WriteLine("Error: " + seh.ToString());
            }
            #endif // _GETSIGNAL

            #if HAVESOMECCODE
            r = 0;
            msg = NULL;
            msg = dbus_msg_new_method_call(serial++,
            "org.freedesktop.DBus", "/org/freedesktop/DBus",
            "org.freedesktop.DBus", "AddMatch");
            if (!msg) {
            dio.io_debug(dio.logpriv, "Unable to create method message for AddMatch\n");
            exit(1);
            }
            MessageInfo::dumpMethodSend(&io_debug, msg);
            dbus_sig signature;
            signature.a[0] = DBUS_STRING;
            signature.a[1] = DBUS_INVALID;
            dbus_msg_set_signature(msg, &signature);
            dbus_msg_body_add(msg, 4096);
            //r |= dbus_msg_body_add_string(msg, "type='signal',interface='com.citrix.xenclient.xenmgr.host'");
            //r |= dbus_msg_body_add_string(msg, "type='method_call'");
            r |= dbus_msg_body_add_string(msg, "type='signal',interface='com.citrix.xenclient.xenmgr.host'");
            r |= dbus_msg_send(&dio, msg);

            loop_dbus(dio, visitor);
            #endif // HAVESOMECCODE
            Console.WriteLine("Press <ENTER> to end connection");
            Console.ReadLine();

            return connector;
        }