public void SerializeRecursive() { var parent = new DummyParent() { A = 1, B = new Dummy() { A = 1, B = 2, C = 3, D = 4, E = 5, F = 6, G = 7, H = 8, I = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 } } }; (BinarySerializer.Serialize(parent).SequenceEqual(new byte[] { 0x01, 0x00, 0x01, 0x02, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 } )).Should().BeTrue(); }
public void Support_child_properties() { var parent = new DummyParent { Child = new DummyChild { Name = "name" } }; var propName = $"{nameof(DummyParent.Child)}.{nameof(DummyChild.Name)}"; parent.CanSetProperty(propName).Should().BeTrue(); const string expected = "John"; parent.SetProperty(propName, expected); parent.Child.Name.Should().Be(expected); parent.Invoking(x => x.CanSetProperty("foo.bar")) .Should().Throw <ArgumentException>() .WithMessage("Property 'foo.bar' does not exist."); parent.Invoking(x => x.SetProperty("foo.bar", "value")) .Should().Throw <ArgumentException>() .WithMessage("Property 'foo.bar' does not exist."); parent.Invoking(x => x.SetProperty("foo", "value")) .Should().Throw <ArgumentException>() .WithMessage("Property 'foo' does not exist."); }
public void SerializeRecursive() { var parent = new DummyParent { A = 1, B = new Dummy { A = 1, B = 2, C = 3, D = 4, E = 5, F = 6, G = 7, H = 8, I = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }, J = 10.9, K = true } }; var ret = new byte[] { 0x01, 0x00, 0x01, 0x02, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x25, 0x40, 0x01, }; _serializer.Serialize(parent).SequenceEqual(ret).Should().BeTrue(); using (var ms = new MemoryStream()) { _serializer.Serialize(parent, ms); ms.ToArray().SequenceEqual(ret).Should().BeTrue(); ms.SetLength(0); using (var mw = new BinaryWriter(ms)) { _serializer.Serialize(parent, mw); ms.ToArray().SequenceEqual(ret).Should().BeTrue(); } } }
/// <summary> /// Kinect がカラー画像を取得したとき実行されるメソッド(イベントハンドラ)。 /// </summary> /// <param name="sender"> /// イベントを通知したオブジェクト。ここでは Kinect になる。 /// </param> /// <param name="e"> /// イベントの発生時に渡されるデータ。ここではカラー画像の情報が含まれる。 /// </param> void ColorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { ColorFrame colorFrame = e.FrameReference.AcquireFrame(); if (colorFrame == null) { return; } int dummyValue = 0; Stopwatch stopWatch = new Stopwatch(); //======================================================= //colorFrame.FrameDescription.Width を 10,000 回参照する。 //======================================================= stopWatch.Restart(); for (int i = 0; i < 10000; i++) { dummyValue = colorFrame.FrameDescription.Width; } stopWatch.Stop(); Console.WriteLine("Pattern-A : " + stopWatch.Elapsed.TotalMilliseconds); //======================================================= //予め確保した colorFrameDescription から Width を参照する。 //======================================================= FrameDescription colorFrameDescription = colorFrame.CreateFrameDescription(ColorImageFormat.Bgra); stopWatch.Restart(); for (int i = 0; i < 10000; i++) { dummyValue = colorFrameDescription.Width; } stopWatch.Stop(); Console.WriteLine("Pattern-B : " + stopWatch.Elapsed.TotalMilliseconds); //======================================================= //予め確保した colorFrameDescription.Width を参照する。 //======================================================= int width = colorFrameDescription.Width; stopWatch.Restart(); for (int i = 0; i < 10000; i++) { dummyValue = width; } stopWatch.Stop(); Console.WriteLine("Pattern-C : " + stopWatch.Elapsed.TotalMilliseconds); //======================================================= //オブジェクトの参照が等しい(≠等値)かどうか調べる。 //======================================================= //FrameDescription は等価のようである。 //Width はプロパティによる実装でアドレスが取得できない。 //新しいインスタンスが生成されていることなどが正しく確認できない。 //(確認する方法ありますかね?) Console.WriteLine(Object.Equals(colorFrame.FrameDescription, colorFrame.FrameDescription)); //======================================================= //参照の類のみで発生するオーバーヘッドでないことを確認する。 //dummyObject.Child.Value を 10,000 回参照する。 //======================================================= DummyParent dummyObject = new DummyParent(); stopWatch.Restart(); for (int i = 0; i < 10000; i++) { dummyValue = dummyObject.Child.Value; } stopWatch.Stop(); Console.WriteLine("Pattern-D : " + stopWatch.Elapsed.TotalMilliseconds); Console.WriteLine("==================================================="); byte[] colors = new byte[this.colorFrameDescription.Width * this.colorFrameDescription.Height * this.colorFrameDescription.BytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray(colors, this.colorImageFormat); BitmapSource bitmapSource = BitmapSource.Create(this.colorFrameDescription.Width, this.colorFrameDescription.Height, 96, 96, PixelFormats.Bgra32, null, colors, this.colorFrameDescription.Width * (int)this.colorFrameDescription.BytesPerPixel); this.canvas.Background = new ImageBrush(bitmapSource); colorFrame.Dispose(); }