public void SetMethod(Object target, MethodInfo method) { ParameterInfo[] parameters = method.GetParameters(); switch (parameters.Length) { case 0: voidMethod = () => method.Invoke(target, new object[] { }); break; case 1: if (parameters[0].ParameterType == typeof(bool)) { boolMethod = (bool b) => method.Invoke(target, new object[] { b }); } else if (parameters[0].ParameterType == typeof(float)) { floatMethod = (float f) => method.Invoke(target, new object[] { f }); } else if (parameters[0].ParameterType == typeof(int)) { intMethod = (int x) => method.Invoke(target, new object[] { x }); } break; } }
public void CacheResultOfMethodThatReturnsObject() { MethodInfo method = new IntMethod(cacheResultTarget.ReturnsScalar).Method; object expectedReturnValue = CacheResultTarget.Scalar; using (mocks.Record()) { ExpectAttributeRetrieval(method); ExpectCacheKeyGeneration(method, null); ExpectCacheInstanceRetrieval("results", resultCache); ExpectCallToProceed(expectedReturnValue); } using (mocks.Playback()) { // return value should be added to cache object returnValue = advice.Invoke(mockInvocation); Assert.AreEqual(expectedReturnValue, returnValue); Assert.AreEqual(1, resultCache.Count); // cached value should be returned object cachedValue = advice.Invoke(mockInvocation); Assert.AreEqual(expectedReturnValue, cachedValue); Assert.AreEqual(1, resultCache.Count); Assert.AreSame(returnValue, cachedValue); } }
public void CacheResultOfMethodThatReturnsObject() { MethodInfo method = new IntMethod(cacheResultTarget.ReturnsScalar).Method; object expectedReturnValue = CacheResultTarget.Scalar; ExpectAttributeRetrieval(method); ExpectCacheKeyGeneration(method, null); ExpectCacheInstanceRetrieval("results", resultCache); ExpectCallToProceed(expectedReturnValue); // return value should be added to cache object returnValue = advice.Invoke((IMethodInvocation)mockInvocation.Object); Assert.AreEqual(expectedReturnValue, returnValue); Assert.AreEqual(1, resultCache.Count); // and again, but without Proceed()... ExpectAttributeRetrieval(method); ExpectCacheKeyGeneration(method, null); ExpectCacheInstanceRetrieval("results", resultCache); // cached value should be returned object cachedValue = advice.Invoke((IMethodInvocation)mockInvocation.Object); Assert.AreEqual(expectedReturnValue, cachedValue); Assert.AreEqual(1, resultCache.Count); Assert.AreSame(returnValue, cachedValue); mockInvocation.Verify(); mockContext.Verify(); }
public void SetMethod(IntMethod method, EventType _eventType = EventType.Always) { eventType = _eventType; intMethod = method; methodParameterCount = 1; if (method != null) { SetMethodParameters(method.Target, method.Method); } }
public void TestAsyncCallWithInts() { int result; IAsyncResult ar; IntMethod m = new IntMethod(IntAdd); ar = m.BeginInvoke(10, 20, null, null); result = m.EndInvoke(ar); AssertEquals("result==30", 30, result); }
public void DisplayDelegate() { //delegate的基本操作 DoFunction doFunction = Show; doFunction.Invoke(); // 可用Invoke來呼叫其委派的所有方法 doFunction(); // 也可以直接呼叫其委派的所有方法 DoFunction2 doFunction2 = ShowText; doFunction2.Invoke("CORN"); doFunction2("CORN"); // 委派呼叫多個方法 doFunction = Show; doFunction += Show2; doFunction(); DoFunction2 doFunction21 = ShowText2; // 可以利用一個委派將其他委派運用一起使用(可加多個("+"),也可以刪除("-")) DoFunction2 sumfunction = doFunction2 + doFunction21; sumfunction.Invoke("Hello Corn!!"); Console.WriteLine("------------------------------------------------------------------"); //將delegate當參數傳遞 DelegateClass02 delegateClass02 = new DelegateClass02(); DelegateClass02.ShowDelegateObj obj = ShowText; delegateClass02.DoAction(obj, "Hi Corn!!"); Console.WriteLine("------------------------------------------------------------------"); IntMethod method = Method01; method += Method02; method += Method03; Console.WriteLine($"{method()}"); foreach (var item in method.GetInvocationList()) //GetInvocationList會將多個方法串成清單 { // DynamicInvoke為取得目前委派的方法 Console.WriteLine(item.DynamicInvoke()); } }
public void CacheResultOfMethodThatReturnsObject() { MethodInfo method = new IntMethod( cacheResultTarget.ReturnsScalar ).Method; object expectedReturnValue = CacheResultTarget.Scalar; ExpectAttributeRetrieval( method ); ExpectCacheKeyGeneration( method, null ); ExpectCacheInstanceRetrieval( "results", resultCache ); ExpectCallToProceed( expectedReturnValue ); // return value should be added to cache object returnValue = advice.Invoke( (IMethodInvocation)mockInvocation.Object ); Assert.AreEqual( expectedReturnValue, returnValue ); Assert.AreEqual( 1, resultCache.Count ); // and again, but without Proceed()... ExpectAttributeRetrieval( method ); ExpectCacheKeyGeneration( method, null ); ExpectCacheInstanceRetrieval( "results", resultCache ); // cached value should be returned object cachedValue = advice.Invoke( (IMethodInvocation)mockInvocation.Object ); Assert.AreEqual( expectedReturnValue, cachedValue ); Assert.AreEqual( 1, resultCache.Count ); Assert.AreSame( returnValue, cachedValue ); mockInvocation.Verify(); mockContext.Verify(); }