private static IReturnsThrows <TMock, TReturn> OutCallbackInternal <TMock, TReturn>(ICallback <TMock, TReturn> mock, object action) where TMock : class { var methodCall = mock.GetType().GetProperty("Setup").GetValue(mock); mock.GetType().Assembly.GetType("Moq.MethodCall") .InvokeMember("SetCallbackResponse", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, methodCall, new[] { action }); return(mock as IReturnsThrows <TMock, TReturn>); }
public void Process(ICallback callback) { if (!(callback is TCallback)) { throw new InvalidOperationException($"Invalid callback type. Got {callback?.GetType()}, expected {typeof(TCallback)}"); } ProcessInternal((TCallback)callback); }
private static IReturnsThrows <TMock, TReturn> OutCallbackInternal <TMock, TReturn>(ICallback <TMock, TReturn> mock, object action) where TMock : class { mock.GetType() .Assembly.GetType("Moq.MethodCall") .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, new[] { action }); return(mock as IReturnsThrows <TMock, TReturn>); }
private static IReturnsThrows <TMock, TReturn> OutCallbackInternal <TMock, TReturn>(ICallback <TMock, TReturn> mock, object action) where TMock : class { mock.GetType().GetTypeInfo() .Assembly.GetType("Moq.MethodCall").GetTypeInfo() .GetDeclaredMethod("SetCallbackWithArguments") .Invoke(mock, new[] { action }); return(mock as IReturnsThrows <TMock, TReturn>); }
// Sashen.Naidoo : 14-02-2012 : This method adds ref support to Moq /// <summary> /// This method can be used to invoke a method call containing a ref parameter in a mocked method /// </summary> /// <typeparam name="TRef"></typeparam> /// <typeparam name="TParam1"></typeparam> /// <typeparam name="TMock"></typeparam> /// <param name="mock"></param> /// <param name="action"></param> /// <returns></returns> public static IReturnsResult <TMock> RefCallback <TRef, TParam1, TMock>(this ICallback mock, RefAction <TRef, TParam1> action) where TMock : class { mock.GetType().Assembly .GetType("Moq.MethodCall") .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, new object[] { action }); return(mock as IReturnsResult <TMock>); }
public static IReturnsThrows <TMock, TReturn> Callback <TMock, TReturn>(this ICallback <TMock, TReturn> mock, Delegate @delegate) where TMock : class { mock.GetType() .Assembly.GetType("Moq.MethodCall") .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, new object[] { @delegate } ); return((IReturnsThrows <TMock, TReturn>)mock); }
public static IReturnsThrows <TMock, TReturn> OutCallback <TMock, TReturn, TIn, TOut>(this ICallback <TMock, TReturn> mock, OutAction <TIn, TOut> action) where TMock : class { mock.GetType() .Assembly.GetType("Moq.MethodCall") .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, new object[] { action }); return(mock as IReturnsThrows <TMock, TReturn>); }
private static IReturnsThrows <TMock, TReturn> OutCallbackInternal <TMock, TReturn>(ICallback <TMock, TReturn> mock, object action) where TMock : class { var method = mock.GetType().GetTypeInfo() .Assembly .GetType("Moq.MethodCall") .GetRuntimeMethods() .SingleOrDefault(m => m.Name == "SetCallbackWithArguments" && !m.IsPublic && !m.IsStatic); method.Invoke(mock, new[] { action }); return(mock as IReturnsThrows <TMock, TReturn>); }
/// <summary> /// Common callback invoker for procedures (i.e. methods that don't return stuff) /// </summary> /// <param name="mock">The mock that this is an extension for.</param> /// <param name="action">The action with out params.</param> /// <returns>The passed mock - for fluent chaining</returns> private static ICallbackResult OutCallbackInternal(ICallback mock, object action) { MethodInfo methodInfo = mock.GetType() .GetTypeInfo() .Assembly.GetType("Moq.MethodCall") .GetTypeInfo() .GetDeclaredMethod("SetCallbackWithArguments"); methodInfo.Invoke(mock, new[] { action }); return((ICallbackResult)mock); }
public static void SetCallbackWithArguments(this ICallback call, Delegate callback) { MethodInfo methodInfo = call.GetType().GetMethod("SetCallbackWithArguments", BindingFlags.NonPublic | BindingFlags.Instance); methodInfo.Invoke(call, new object[] { callback }); }
/// <summary> /// Вызов внутренней "магии" Moq для подписки на нужный Callback (вариант без возвращаемого значения). /// </summary> /// <param name="mock"> /// <see cref="Moq"/>, для которого нужно вызвать "магию". /// </param> /// <param name="action"> /// <see cref="Action{T}"/> с ref-параметром. /// </param> /// <returns> /// Результат выполнения Callback. /// </returns> private static ICallbackResult RefCallbackInternal(ICallback mock, object action) { mock.GetType().Assembly.GetType("Moq.MethodCall") .InvokeMember("SetCallbackWithArguments", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, mock, new[] { action }); return((ICallbackResult)mock); }
/// <summary> /// 注册回调类 /// </summary> /// <param name="name">配置项名称</param> /// <param name="callback">回调类的实例</param> public static void RegisterCallback(string name, ICallback callback) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); } if (callback == null) { throw new ArgumentNullException("callback"); } if (!CallbackContainer.ContainsKey(name)) { CallbackContainer.Add(name, callback); LogManager.GetLogger().Info(string.Format("DisconfClient.ConfigCallbackManager.RegisterCallback(name={0},callback={1}", name, callback.GetType().GetFullTypeName())); } }