public static void DoesNotThrow(MyTestDelegate d) { try { d(); } catch (Exception e) { Assert.Fail(e.ToString()); } }
/// <summary> /// 测试委托实现异步,异步编程模型APM /// </summary> public static void TestRun() { ThreadPool.SetMaxThreads(500, 500); PrintMessage("Main Thread Start"); //使用委托异步 MyTestDelegate d1 = new MyTestDelegate(AsyncMethod1); // 异步调用委托 IAsyncResult result = d1.BeginInvoke((ar)=>Console.WriteLine($"{ar.AsyncState}\n{ar.CompletedSynchronously}\n{ar.IsCompleted}"), null); // 获取结果并打印出来 //string strData = d1.EndInvoke(result); //Console.WriteLine(strData); Console.WriteLine("Main Thread End"); Console.ReadKey(); }
public static void Throws <T>(MyTestDelegate d) where T : Exception { try { d(); } catch (Exception e) { #if USEING_NUNIT Assert.IsInstanceOf <T>(e); #else Assert.IsInstanceOfType(e, typeof(T)); #endif } }
//这个方法接收一个delegate类型的参数,也就是接收一个函数作为参数 public static void ReceiveDelegateArgsFunc(MyTestDelegate func) { func(21); }