private void DirectOutputTest() { Stopwatch sw = Stopwatch.StartNew(); int total = 0; ReflectClass1 testClass = new ReflectClass1(random); for (int i = 0; i < Iterations; i++) { total += testClass.Prop1; total += testClass.Prop2; total += testClass.Prop3; total += testClass.Prop4; total += testClass.Prop5; total += testClass.Prop6; total += testClass.Prop7; total += testClass.Prop8; total += testClass.Prop9; total += testClass.Prop10; } Console.WriteLine(string.Format(" End direct output benchmark. iterations = {0:N0} end time: {1:0.000}", Iterations, sw.ElapsedMilliseconds / 1000.0)); Console.WriteLine(total); }
private void UseDelegateList() { //setup delegateList foreach (var propertyInfo in typeof(ReflectClass1).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Func <ReflectClass1, int> getPropDelegate = (Func <ReflectClass1, int>)Delegate.CreateDelegate(typeof(Func <ReflectClass1, int>), null, propertyInfo.GetGetMethod()); delegateList.Add(getPropDelegate); } Stopwatch sw = Stopwatch.StartNew(); ReflectClass1 testClass = new ReflectClass1(random); int total = 0; for (int i = 0; i < Iterations; i++) { foreach (var getProp in delegateList) { total += getProp(testClass); } } Console.WriteLine(string.Format(" End delegate performance test. iterations = {0:N0} end time: {1:0.000}", Iterations, sw.ElapsedMilliseconds / 1000.0)); Console.WriteLine(total); }
private void UseDelegateList() { //setup delegateList TestMethod2 <ReflectClass1>(); Stopwatch sw = Stopwatch.StartNew(); ReflectClass1 testClass = new ReflectClass1(random); int total = 0; for (int i = 0; i < Iterations; i++) { foreach (PropertyInfo propertyInfo in typeof(ReflectClass1).GetProperties()) { if (delegateList.ContainsKey(propertyInfo.Name)) { Func <ReflectClass1, int> getPropDelegate = (Func <ReflectClass1, int>)delegateList[propertyInfo.Name]; total += getPropDelegate(testClass); } } } Console.WriteLine(string.Format(" End delegate performance test. iterations = {0:N0} end time: {1:0.000}", Iterations, sw.ElapsedMilliseconds / 1000.0)); Console.WriteLine(total); }
private void UseDirectReflection() { Stopwatch sw = Stopwatch.StartNew(); int total = 0; ReflectClass1 testClass = new ReflectClass1(random); for (int i = 0; i < Iterations; i++) { foreach (PropertyInfo propertyInfo in typeof(ReflectClass1).GetProperties()) { if (propertyInfo == null) { continue; } total += (int)propertyInfo.GetValue(testClass, null); } } Console.WriteLine(string.Format(" End direct reflection performance test. iterations = {0:N0} end time: {1:0.000}", Iterations, sw.ElapsedMilliseconds / 1000.0)); Console.WriteLine(total); }