private static void CompareReflectAndEmitAccess() { Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("======= Compare Reflect and Emit Access performance ======="); var viewModel = new TestViewModel(); const int count = 80000; var reflectManager = new ReflectManager(); var emitManager = new EmitManager(); viewModel.TestViewModelEvent += viewModel_TestViewModelEvent; // 1. SetProperty var sw = new Stopwatch(); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetProperty(viewModel, "Age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("1. SetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetProperty(viewModel, "Age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("1. SetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 2. GetProperty sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetProperty(viewModel, "Age"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("2. GetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetProperty(viewModel, "Age"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("2. GetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 3. Get Index property sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetIndexProperty(viewModel, 1); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("3. GetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetIndexProperty(viewModel, 1); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("3. GetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 4. Set Index property sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetIndexProperty(viewModel, i, i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("4. SetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetIndexProperty(viewModel, i, i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("4. SetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 5. Create Instance sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.CreateInstance<TestViewModel>(); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("5. CreateInstance<T> Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.CreateInstance<TestViewModel>(); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("5. CreateInstance<T> Emit time elapsed {0}", sw.ElapsedMilliseconds); // 6. Get method info sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("6. GetMethodInfo Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("6. GetMethodInfo Emit time elapsed {0}", sw.ElapsedMilliseconds); // 7. Get Field sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetField(viewModel, "_name"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("7. GetField Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetField(viewModel, "_name"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("7. GetField Emit time elapsed {0}", sw.ElapsedMilliseconds); // 8. Set Field sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetField(viewModel, "_age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("8. SetField Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetField(viewModel, "_age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("8. SetField Emit time elapsed {0}", sw.ElapsedMilliseconds); // 9. Invoke Method sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.InvokeMethod(viewModel, "SetAge", new object[] { i }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("9. Invoke method Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.InvokeMethod(viewModel, "SetAge", new object[] { i }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("9. Invoke method Emit time elapsed {0}", sw.ElapsedMilliseconds); // 10. Raise Event sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.RaiseEvent(viewModel, "TestViewModelEvent", null); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("10. Raise event Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.RaiseEvent(viewModel, "TestViewModelEvent", null); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("10. Raise event Emit time elapsed {0}", sw.ElapsedMilliseconds); }
public void Test_EmitManager_Method() { var manager = new EmitManager(); var te = manager.CreateInstance <TestEmit>(); Assert.IsNull(te.Name); manager.InvokeMethod(te, "SetAge", new object[] { 9 }); Assert.AreEqual(9, te.Age); var age1 = (int)manager.InvokeMethod(te, "GetAge", null); Assert.AreEqual(9, age1); string propertyName = null; te.PropertyChanged += (sender, args) => { propertyName = args.PropertyName; }; var methodInfo = manager.GetMethodInfo(te.GetType(), "OnPropertyChanged", new object[] { "parameter" }); Assert.IsNotNull(methodInfo); manager.RaiseEvent(te, "PropertyChanged", new PropertyChangedEventArgs("Age")); Assert.AreEqual(propertyName, "Age"); Exception exception = null; try { manager.InvokeMethod(te, "GetAgeEx", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); exception = null; try { manager.RaiseEvent(te, "NoEvent", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension)); methodInfo = manager.GetMethodInfo(te.GetType(), "GetAgeEx", null); Assert.IsNotNull(methodInfo); var age = (int)manager.InvokeMethod(te, "GetAgeEx", null); Assert.AreEqual(9, age); manager.UnRegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension)); exception = null; try { manager.InvokeMethod(te, "GetAgeEx", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); te.Name2 = "2"; exception = null; try { DynamicEngine.RaiseEvent(te, "name2", new PropertyChangedEventArgs("Age")); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2)); age = (int)manager.InvokeMethod(te, "GetAgeEx2", null); Assert.AreEqual(9, age); TestEmit.AgeEx = 0; object age2 = manager.InvokeMethod(typeof(TestEmit), "AddAgeEx", null); Assert.AreEqual(1, age2); age2 = manager.InvokeMethod(te, manager.GetMethodInfo(typeof(TestEmit), "AddAgeEx", null), null); Assert.AreEqual(2, age2); age2 = manager.InvokeMethod(typeof(TestEmit), "SetAgeEx", new object[] { 5 }); Assert.AreEqual(5, age2); age2 = manager.InvokeMethod(te, "SetAgeEx", new object[] { 7 }); Assert.AreEqual(7, age2); const string name = "yohan"; var parameters = new object[] { name, 1 }; manager.InvokeMethod(te, "ChangeProperty", parameters); Assert.AreEqual("yohan1", parameters[0]); parameters = new object[] { name }; manager.InvokeMethod(te, "ChangeProperty", parameters); Assert.AreEqual("yohan", parameters[0]); }
public void Test_EmitManager_Method() { var manager = new EmitManager(); var te = manager.CreateInstance<TestEmit>(); Assert.IsNull(te.Name); manager.InvokeMethod(te, "SetAge", new object[] { 9 }); Assert.AreEqual(9, te.Age); var age1 = (int)manager.InvokeMethod(te, "GetAge", null); Assert.AreEqual(9, age1); string propertyName = null; te.PropertyChanged += (sender, args) => { propertyName = args.PropertyName; }; var methodInfo = manager.GetMethodInfo(te.GetType(), "OnPropertyChanged", new object[] { "parameter" }); Assert.IsNotNull(methodInfo); manager.RaiseEvent(te, "PropertyChanged", new PropertyChangedEventArgs("Age")); Assert.AreEqual(propertyName, "Age"); Exception exception = null; try { manager.InvokeMethod(te, "GetAgeEx", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); exception = null; try { manager.RaiseEvent(te, "NoEvent", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension)); methodInfo = manager.GetMethodInfo(te.GetType(), "GetAgeEx", null); Assert.IsNotNull(methodInfo); var age = (int)manager.InvokeMethod(te, "GetAgeEx", null); Assert.AreEqual(9, age); manager.UnRegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension)); exception = null; try { manager.InvokeMethod(te, "GetAgeEx", null); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); te.Name2 = "2"; exception = null; try { DynamicEngine.RaiseEvent(te, "name2", new PropertyChangedEventArgs("Age")); } catch (MissingMemberException e) { exception = e; } Assert.IsNotNull(exception); manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2)); age = (int)manager.InvokeMethod(te, "GetAgeEx2", null); Assert.AreEqual(9, age); TestEmit.AgeEx = 0; object age2 = manager.InvokeMethod(typeof(TestEmit), "AddAgeEx", null); Assert.AreEqual(1, age2); age2 = manager.InvokeMethod(te, manager.GetMethodInfo(typeof(TestEmit), "AddAgeEx", null), null); Assert.AreEqual(2, age2); age2 = manager.InvokeMethod(typeof(TestEmit), "SetAgeEx", new object[] { 5 }); Assert.AreEqual(5, age2); age2 = manager.InvokeMethod(te, "SetAgeEx", new object[] { 7 }); Assert.AreEqual(7, age2); const string name = "yohan"; var parameters = new object[] { name, 1 }; manager.InvokeMethod(te, "ChangeProperty", parameters); Assert.AreEqual("yohan1", parameters[0]); parameters = new object[] { name }; manager.InvokeMethod(te, "ChangeProperty", parameters); Assert.AreEqual("yohan", parameters[0]); }
private static void CompareReflectAndEmitAccess() { Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("======= Compare Reflect and Emit Access performance ======="); var viewModel = new TestViewModel(); const int count = 80000; var reflectManager = new ReflectManager(); var emitManager = new EmitManager(); viewModel.TestViewModelEvent += viewModel_TestViewModelEvent; // 1. SetProperty var sw = new Stopwatch(); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetProperty(viewModel, "Age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("1. SetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetProperty(viewModel, "Age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("1. SetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 2. GetProperty sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetProperty(viewModel, "Age"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("2. GetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetProperty(viewModel, "Age"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("2. GetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 3. Get Index property sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetIndexProperty(viewModel, 1); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("3. GetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetIndexProperty(viewModel, 1); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("3. GetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 4. Set Index property sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetIndexProperty(viewModel, i, i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("4. SetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetIndexProperty(viewModel, i, i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("4. SetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds); // 5. Create Instance sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.CreateInstance <TestViewModel>(); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("5. CreateInstance<T> Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.CreateInstance <TestViewModel>(); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("5. CreateInstance<T> Emit time elapsed {0}", sw.ElapsedMilliseconds); // 6. Get method info sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("6. GetMethodInfo Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("6. GetMethodInfo Emit time elapsed {0}", sw.ElapsedMilliseconds); // 7. Get Field sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.GetField(viewModel, "_name"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("7. GetField Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.GetField(viewModel, "_name"); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("7. GetField Emit time elapsed {0}", sw.ElapsedMilliseconds); // 8. Set Field sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.SetField(viewModel, "_age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("8. SetField Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.SetField(viewModel, "_age", i); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("8. SetField Emit time elapsed {0}", sw.ElapsedMilliseconds); // 9. Invoke Method sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.InvokeMethod(viewModel, "SetAge", new object[] { i }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("9. Invoke method Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.InvokeMethod(viewModel, "SetAge", new object[] { i }); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("9. Invoke method Emit time elapsed {0}", sw.ElapsedMilliseconds); // 10. Raise Event sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { reflectManager.RaiseEvent(viewModel, "TestViewModelEvent", null); } sw.Stop(); Console.ForegroundColor = ConsoleColor.White; System.Console.WriteLine("10. Raise event Reflect time elapsed {0}", sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); for (int i = 0; i < count; i++) { emitManager.RaiseEvent(viewModel, "TestViewModelEvent", null); } sw.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("10. Raise event Emit time elapsed {0}", sw.ElapsedMilliseconds); }