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>);
        }
Example #2
0
        public void Process(ICallback callback)
        {
            if (!(callback is TCallback))
            {
                throw new InvalidOperationException($"Invalid callback type. Got {callback?.GetType()}, expected {typeof(TCallback)}");
            }

            ProcessInternal((TCallback)callback);
        }
Example #3
0
        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>);
 }
Example #5
0
 // 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>);
 }
Example #6
0
 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);
 }
Example #7
0
 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>);
 }
Example #8
0
        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>);
        }
Example #9
0
        /// <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);
        }
Example #10
0
    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 });
    }
Example #11
0
 /// <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);
 }
Example #12
0
 /// <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()));
     }
 }