public void ShouldReadKnownFields() { var mva = new MemberValueAccessor(typeof(TestA).GetFields()); var a1 = CreateTestObject(); mva.GetValue("Field1", a1).Should().Be(23.34); }
public void ShouldWriteKnownFields() { var mva = new MemberValueAccessor(typeof(TestA).GetFields()); var a1 = CreateTestObject(); mva.SetValue("Field1", a1, 0.25); mva.GetValue("Field1", a1).Should().Be(0.25); }
public void ShouldReadKnownProperties() { var mva = new MemberValueAccessor(typeof(TestA).GetProperties()); var a1 = CreateTestObject(); mva.GetValue("PropString", a1).Should().Be("test"); mva.GetValue("PropInt", a1).Should().Be(48); }
public void ShouldWriteKnownProperties() { var mva = new MemberValueAccessor(typeof(TestA).GetProperties()); var a1 = CreateTestObject(); mva.SetValue("PropString", a1, "new value"); mva.GetValue("PropString", a1).Should().Be("new value"); mva.SetValue("PropInt", a1, 10); mva.GetValue("PropInt", a1).Should().Be(10); }
public void TestMemberValueAccessorsPerfomance() { const int total = 1000000; TestA a = new TestA { PropString = "test", PropInt = 48 }; var sw = Stopwatch.StartNew(); for (int i = 0; i < total; i++) { string v = a.PropString; a.PropString = "new data"; int i1 = a.PropInt; a.PropInt = i; } sw.Stop(); Console.WriteLine("Native C# time: " + sw.ElapsedMilliseconds); var mva = new MemberValueAccessor(typeof(TestA).GetProperties()); sw.Restart(); for (int i = 0; i < total; i++) { object v = mva.GetValue("PropString", a); mva.SetValue("PropString", a, "new data"); object i1 = mva.GetValue("PropInt", a); mva.SetValue("PropInt", a, i); } sw.Stop(); Console.WriteLine("FastRT time: " + sw.ElapsedMilliseconds); PropertyInfo pStr = a.GetType().GetProperty("PropString"); PropertyInfo pInt = a.GetType().GetProperty("PropInt"); sw.Restart(); for (int i = 0; i < total; i++) { object v = pStr.GetValue(a, null); pStr.SetValue(a, "new data", null); object i1 = pInt.GetValue(a, null); pInt.SetValue(a, i); } sw.Stop(); Console.WriteLine("Reflection time: " + sw.ElapsedMilliseconds); }