private static ObjectValues Parse(Object source) { var formatter = new BinaryFormatter(); var stream = new MemoryStream(); formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); var hex = BitConverter.ToString(stream.ToArray()).Replace("-", ""); Console.WriteLine(hex); var reader = new BinaryFormatterStreamReader(); return reader.Read(stream); }
public void ParseSimpleConsoleApplicationClass() { var bytes = new Byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x49, 0x43, 0x6F, 0x6E, 0x73, 0x6F, 0x6C, 0x65, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2C, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x31, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20, 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D, 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C, 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B, 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D, 0x6E, 0x75, 0x6C, 0x6C, 0x05, 0x01, 0x00, 0x00, 0x00, 0x1C, 0x43, 0x6F, 0x6E, 0x73, 0x6F, 0x6C, 0x65, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2E, 0x54, 0x65, 0x73, 0x74, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x02, 0x00, 0x00, 0x00, 0x07, 0x4D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x05, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x01, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x0C, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x39, 0x05, 0x00, 0x00, 0x0B }; var stream = new MemoryStream(bytes); var reader = new BinaryFormatterStreamReader(); var obj = reader.Read(stream); Assert.That(obj, Is.Not.Null); Assert.That(obj.AssemblyName, Is.EqualTo("ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); Assert.That(obj.TypeName, Is.EqualTo("ConsoleApplication.TestClass")); Assert.That(obj["Message"], Is.EqualTo("Hello world!")); Assert.That(obj["Value"], Is.EqualTo(1337)); }