public void InvokeOnMainLoop(GuiUnit.InvokerHelper helper) { var invokeOnMainThreadMethods = Application.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); var invokeOnMainThreadMethod = invokeOnMainThreadMethods.First(d => d.Name == "Invoke" && d.GetParameters().Length == 1); EventHandler invoker = delegate { helper.Invoke(); }; invokeOnMainThreadMethod.Invoke(null, new [] { invoker }); }
/// <summary> /// Invoke a method, converting any TargetInvocationException to an NUnitException. /// </summary> /// <param name="method">A MethodInfo for the method to be invoked</param> /// <param name="fixture">The object on which to invoke the method</param> /// <param name="args">The argument list for the method</param> /// <returns>The return value from the invoked method</returns> public static object InvokeMethod(MethodInfo method, object fixture, params object[] args) { if (method != null) { if (fixture == null && !method.IsStatic) { Console.WriteLine("Trying to call {0} without an instance", method.Name); } try { if (LocationProperty != null) { Environment.CurrentDirectory = System.IO.Path.GetDirectoryName((string)LocationProperty.GetValue(method.DeclaringType.Assembly, null)); } object result = null; if (GuiUnit.TestRunner.MainLoop == null) { result = method.Invoke(fixture, args); } else { var invokeHelper = new GuiUnit.InvokerHelper { Context = TestExecutionContext.CurrentContext, Func = () => method.Invoke(fixture, args) }; GuiUnit.TestRunner.MainLoop.InvokeOnMainLoop(invokeHelper); invokeHelper.Waiter.WaitOne(); if (invokeHelper.ex != null) { Rethrow(invokeHelper.ex); } result = invokeHelper.Result; } if (result is System.Threading.Tasks.Task) { ((System.Threading.Tasks.Task)result).Wait(); } return(result); } catch (Exception e) { Rethrow(e); } } return(null); }