public static void Execute(string[] args) { var methodExecutor = new DynamicMethodExecutor(); var keepAliveOnError = args.Any(x => x.Equals("KeepAliveOnError", StringComparison.OrdinalIgnoreCase)); var shouldExit = false; Console.WriteLine("Console Multi Function Application"); Console.WriteLine($"Process Info: {Process.GetCurrentProcess().ProcessName} {Process.GetCurrentProcess().Id}"); Console.WriteLine($"Keep Alive on Error: {keepAliveOnError}"); Console.WriteLine(); while (!shouldExit) { Console.Write($"{DateTime.Now.ToLongTimeString()} >"); var command = Console.ReadLine().Trim(); if (string.IsNullOrWhiteSpace(command)) { continue; } var startPos = 0; for (startPos = 0; startPos < command.Length; startPos++) { if (char.IsLetter(command[startPos])) { break; } } command = command.Substring(startPos); if (string.IsNullOrWhiteSpace(command)) { continue; } Console.WriteLine(); if (command.Equals("exit", StringComparison.OrdinalIgnoreCase) || command.Equals("quit", StringComparison.OrdinalIgnoreCase)) { shouldExit = true; continue; } if (command.Equals("help", StringComparison.OrdinalIgnoreCase) || command.Equals("usage", StringComparison.OrdinalIgnoreCase) || command.Equals("?")) { PrintUsage(); continue; } ExecuteCommand(methodExecutor, keepAliveOnError, command); } }
public void ExecuteTest() { try { Type mockType = typeof(Mock); var mock = new Mock(); var set = mockType.GetProperty("Sum").GetSetMethod(); DynamicMethodExecutor executor = new DynamicMethodExecutor(set); Action action = () => { for (int i = 0; i < 100000; i++) { executor.Execute(mock, new object[] { i }); } }; this.TestContext.WriteLine(action.Elapsed(1, false).ToString()); } catch (Exception ex) { TestContext.WriteLine(ex.ToString()); TestContext.WriteLine(ex.GetType().Name); } try { Type mockType = typeof(Mock); var mock = new Mock(); var set = mockType.GetProperty("Sum").GetSetMethod(); Action action = () => { for (int i = 0; i < 100000; i++) { set.Invoke(mock, new object[] { i }); } }; this.TestContext.WriteLine(action.Elapsed(1, false).ToString()); } catch (Exception ex) { TestContext.WriteLine(ex.ToString()); TestContext.WriteLine(ex.GetType().Name); } }
static void Main(string[] args) { int times = 2; DynamicMethodExecutor executor = new DynamicMethodExecutor(typeof(Program).GetMethod("Call")); Stopwatch watch3 = new Stopwatch(); watch3.Start(); for (int i = 0; i < times; i++) { executor.Execute(new Program(), new object[3] { "test", "", "" }); } watch3.Stop(); Console.WriteLine(watch3.Elapsed + " (Dynamic executor)"); ModelBinderFactory.Current.SetModelBinder(new DefaultModelBinder());//a way set selfdefined modelbinder,you can ignore it IConfigurationSource src = ConfigurationManager.GetSection("DataAccess") as IConfigurationSource; DriverBase db = DatabaseFactory.CreateDriver(src); //DataTable dt = db.GetDataTable("select employeeid,lastname,firstname from dbo.Employees"); //IEnumerable<Employee> list = db.FindAll<Employee>("select employeeid,lastname,firstname from dbo.Employees"); //int count = 0; //IEnumerable<Employee> list = db.PagerList<Employee>("select employeeid,lastname,firstname from dbo.Employees", 1, count, "employeeid"); System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); //int count; //IEnumerable<Blog> list = db.SetPageSize(1) // .PagerList<Blog>("select * from blog", 1,out count); // DataTable dt = db.GetDataTable("select * from blog"); IEnumerable <Blog> list = db.FindAll <Blog>("select * from blog"); timer.Stop(); Console.WriteLine("first timespan:" + timer.Elapsed); timer.Reset(); timer.Start(); list = db.FindAll <Blog>("select * from blog"); timer.Stop(); Console.WriteLine("second timespan:" + timer.Elapsed); // list = db.FindAll<Employee>("select employeeid,lastname,firstname from dbo.Employees"); //foreach (Employee model in DefaultModelBinder.BindModel<Employee>(dt)) //{ // Console.WriteLine(model.FirstName); //} //Console.WriteLine("Count:"+count); //foreach (Blog model in list) //{ // Console.WriteLine(model.Name); //} //foreach (DataRow dr in dt.Rows) //{ // foreach (DataColumn dc in dt.Columns) // { // Console.Write(dc.ColumnName + ":" + dr[dc.ColumnName] + "\t"); // } // Console.WriteLine(); //} //Console.WriteLine("Run 1 - configuration with xml file"); //using (WindsorContainer container = new WindsorContainer("castle.config")) //{ // container.AddFacility<LoggingFacility>(f => f.UseLog4Net()); // ISomething something = container.Resolve<ISomething>(); // something.DoSomething(""); // Console.WriteLine("Augment 10 returns " + something.Augment(10)); //} //Console.WriteLine("Run 2 - configuration fluent"); //using (WindsorContainer container = new WindsorContainer()) //{ // container.Register( // Component.For<IInterceptor>() // .ImplementedBy<BetterDumpInterceptor>() // .Named("myinterceptor")); // container.Register( // Component.For<ISomething>() // .ImplementedBy<Something>() // .Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere); // ISomething something = container.Resolve<ISomething>(); // something.DoSomething(""); // Console.WriteLine("Augment 10 returns " + something.Augment(10)); //} //Console.WriteLine("Run 3 - configuration fluent"); //using (WindsorContainer container = new WindsorContainer()) //{ // Dictionary<Type, List<String>> RegexSelector = new Dictionary<Type, List<string>>(); // RegexSelector.Add(typeof(DumpInterceptor), new List<string>() { "DoSomething" }); // RegexSelector.Add(typeof(LogInterceptor), new List<string>() { "Augment" }); // InterceptorSelector selector = new InterceptorSelector(RegexSelector); // container.Register( // Component.For<IInterceptor>() // .ImplementedBy<DumpInterceptor>() // .Named("myinterceptor")); // container.Register( // Component.For<IInterceptor>() // .ImplementedBy<LogInterceptor>() // .Named("LogInterceptor")); // container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config")); // container.Register( // Component.For<ISomething>() // .ImplementedBy<Something>() // .Interceptors(InterceptorReference.ForKey("myinterceptor") , // InterceptorReference.ForKey("LogInterceptor") // ) // .SelectedWith(selector).Anywhere); // ISomething something = container.Resolve<ISomething>(); // something.DoSomething(""); // Console.WriteLine("Augment 10 returns " + something.Augment(10)); //} Console.ReadKey(); }
static void ExecuteCommand(DynamicMethodExecutor methodExecutor, bool keepAliveOnError, string commandText) { Logger.Info($"EXECUTING: '{commandText}'"); var args = commandText.Split(' '); if (args.Count() < 2) { Logger.Error($"INVALID FORMAT: '{commandText}'"); PrintUsage(); if (!keepAliveOnError) { Environment.Exit(-1); } return; } var libraryName = args[0]; var methodName = args[1]; var methodParams = args.Skip(2).ToArray(); var countMethodParams = methodParams.Count(); var matchedMethods = ReflectionUtil.FindMethodUsingAttributes <LibraryAttribute, LibraryMethodAttribute>(libraryName, methodName) .Where(x => x.GetParameters().Count() == countMethodParams) .ToArray(); if (matchedMethods.Length == 0) { Logger.Error($"\tMETHOD MISMATCH: Unable to find method {libraryName}.{methodName} that accepts {countMethodParams} parameters."); PrintUsage(); if (!keepAliveOnError) { Environment.Exit(-2); } return; } if (matchedMethods.Length > 1) { Logger.Error($"\tMETHOD MISMATCH: Ambiguous Match on {libraryName}.{methodName} with {countMethodParams} parameters"); PrintUsage(); if (!keepAliveOnError) { Environment.Exit(-3); } return; } try { methodExecutor.ExecuteDynamicMethod(matchedMethods[0], methodParams); } catch (Exception ex) { Logger.Error($"\tEXECUTION ERROR: {ex}"); if (!keepAliveOnError) { Environment.Exit(-4); } return; } }