public byte[] Serialize(BuildInTypesDto dto)
 {
     using (var stream = new System.IO.MemoryStream()) //http://stackoverflow.com/questions/14196488/protobuf-net-error-message-invalid-field-in-source-data-0
     {
         Serializer.Serialize<BuildInTypesDto>(stream, dto);
         return stream.ToArray(); //There is some extra garbage in buffer when returning. More on: http://stackoverflow.com/questions/14196488/protobuf-net-error-message-invalid-field-in-source-data-0
     }
 }
 public byte[] Serialize(BuildInTypesDto dto)
 {
     using (var stream = new System.IO.MemoryStream())
     {
         JsonSerializer.SerializeToStream<BuildInTypesDto>(dto, stream);
         return stream.GetBuffer();
     }
 }
 public byte[] Serialize(BuildInTypesDto dto)
 {
     using (var stream = new MemoryStream())
     {
         var serializer = new BinaryFormatter();
         serializer.Serialize(stream, dto);
         return stream.GetBuffer();
     }
 }
 public byte[] Serialize(BuildInTypesDto dto)
 {
     using (var stream = new System.IO.MemoryStream())
     {
         XmlSerializer serializer = new XmlSerializer(typeof(BuildInTypesDto));
         serializer.Serialize(stream, dto);
         Console.WriteLine(StreamToString(stream));
         return stream.GetBuffer();
     }
 }
        public static BuildInTypesDto Create(string type)
        {
            Console.WriteLine("Processing serialization in " + type);
            var dto = new BuildInTypesDto();
            Random r = new Random();
            TimeSpan span = TimeSpan.FromHours(24);
            if (type == "Binary Serializer" || type == "JSON Serializer" || type == "Protobuf Serializer")
            {
                dto.BinaryNJSONNProtobufSpans = new TimeSpan[100000];
            }
            if (type == "Binary Serializer")
            {
                dto.BinaryColors = new Color[100000];
            }
            if (type == "Binary Serializer" || type == "JSON Serializer")
            {
                dto.BinaryNJSONTimeOffsets = new DateTimeOffset[100000];
            }
            if (type == "Xml Serializer")
            {
                dto.XmlColors = new string[100000];
                dto.XmlSpans = new long[100000];
            }
            if (type == "Xml Serializer" || type == "Protobuf Serializer")
            {
                dto.XmlNProtobufTimeOffsets = new string[100000];
            }
            if (type == "Protobuf Serializer" || type == "JSON Serializer")
            {
                dto.ProtoNJSONColors = new int[100000];
            }
            //dto.Offsets = new DateTimeOffset[100000];
            KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));

            for (int i = 0; i <= 9999; i++)
            {
                int randomSeconds = r.Next((int)span.TotalSeconds);
                KnownColor randomColorName = names[r.Next(names.Length)];

                if (type == "Binary Serializer")
                {
                    dto.BinaryNJSONNProtobufSpans[i] = TimeSpan.FromSeconds(randomSeconds); //passed
                    dto.BinaryColors[i] = Color.FromKnownColor(randomColorName); //passed
                    dto.BinaryNJSONTimeOffsets[i] = new DateTimeOffset(r.Next(1, 2008), r.Next(1, 12), r.Next(1, 28), r.Next(1, 23), r.Next(1, 59), r.Next(1, 59),
                                 new TimeSpan(1, 0, 0)); //passed
                }
                else if (type == "Protobuf Serializer")
                {
                    dto.BinaryNJSONNProtobufSpans[i] = TimeSpan.FromSeconds(randomSeconds); //passed
                    dto.ProtoNJSONColors[i] = (Color.FromKnownColor(randomColorName)).ToArgb(); //FAILED!
                    dto.XmlNProtobufTimeOffsets[i] = (new DateTimeOffset(r.Next(1, 2008), r.Next(1, 12), r.Next(1, 28), r.Next(1, 23), r.Next(1, 59), r.Next(1, 59),
                                 new TimeSpan(1, 0, 0))).ToString(); //FAILED!
                }
                else if (type == "JSON Serializer")
                {
                    dto.BinaryNJSONNProtobufSpans[i] = TimeSpan.FromSeconds(randomSeconds);
                    dto.ProtoNJSONColors[i] = (Color.FromKnownColor(randomColorName)).ToArgb();
                    dto.BinaryNJSONTimeOffsets[i] = new DateTimeOffset(r.Next(1, 2008), r.Next(1, 12), r.Next(1, 28), r.Next(1, 23), r.Next(1, 59), r.Next(1, 59),
                                 new TimeSpan(1, 0, 0));
                }
                else //Xml Serializer
                {
                    dto.XmlSpans[i] = (TimeSpan.FromSeconds(randomSeconds)).Ticks; //FAILED!
                    dto.XmlColors[i] = ColorTranslator.ToHtml(Color.FromKnownColor(randomColorName)); //FAILED!
                    dto.XmlNProtobufTimeOffsets[i] = (new DateTimeOffset(r.Next(1, 2008), r.Next(1, 12), r.Next(1, 28), r.Next(1, 23), r.Next(1, 59), r.Next(1, 59),
                                 new TimeSpan(1, 0, 0))).ToString(); //FAILED!
                }
            }
            return dto;
        }