コード例 #1
0
 public WeakAction(Action action)
 {
     action.ThrowIfNull("action");
     _method = action.GetMethodInfo();
     if (action.GetMethodInfo().IsStatic)
     {
         _staticAction = action;
     }
     if (action.Target != null)
     {
         _actionTargetReference = new WeakReference(action.Target);
     }
 }
コード例 #2
0
ファイル: EventUtilities.cs プロジェクト: yukiyuki/MyToolkit
 /// <summary>Registers a static event on the given target object. </summary>
 /// <param name="type">The target type. </param>
 /// <param name="eventName">The event name. </param>
 /// <param name="callback">The callback. </param>
 /// <returns>The registration token to deregister the event. </returns>
 public static object RegisterStaticEvent(Type type, string eventName, Action<object, object> callback)
 {
     var callbackMethodInfo = callback.GetMethodInfo();
     var eventInfo = type.GetRuntimeEvent(eventName);
     var callbackDelegate = callbackMethodInfo.CreateDelegate(eventInfo.EventHandlerType, callback.Target);
     return eventInfo.AddMethod.Invoke(null, new object[] { callbackDelegate });
 }
コード例 #3
0
        private IInvocation CreateInvocationThatThrows()
        {
            var array = new[]
                        {
                            1,
                            2
                        };

            var arguments = new object[]
                            {
                                array
                            };
            var action = new Action(DoesThrow);
            var invocation = Substitute.For <IInvocation>();
            invocation.TargetType.Returns(typeof ( LogAspectTests ));
            invocation.Method.Returns(action.GetMethodInfo());
            invocation.Arguments.Returns(arguments);
            invocation.When(x => x.Proceed())
                      .Do(x =>
                          {
                              DoesThrow();
                          });

            return invocation;
        }
コード例 #4
0
 public WeakRelayCommand(Action<object> executeCallback, Func<object, bool> canExecuteCallback = null)
 {
     ExecuteTargetReference = new WeakReference<object>(executeCallback.Target);
     if (canExecuteCallback != null)
         CanExecuteTargetReference = new WeakReference<object>(canExecuteCallback.Target);
     ExecuteCallbackInfo = executeCallback.GetMethodInfo();
     CanExecuteCallbackInfo = canExecuteCallback?.GetMethodInfo();
 }
コード例 #5
0
        public WeakAction(object target, Action action)
        {
            #if NETFX_CORE
            if (action.GetMethodInfo().IsStatic)
            #else
            if (action.Method.IsStatic)
            #endif
            {
                _staticAction = action;

                if (target != null)
                {
                    // Keep a reference to the target to control the
                    // WeakAction's lifetime.
                    Reference = new WeakReference(target);
                }

                return;
            }

            #if SILVERLIGHT
            if (!action.Method.IsPublic
                || (action.Target != null
                    && !action.Target.GetType().IsPublic
                    && !action.Target.GetType().IsNestedPublic))
            {
                _action = action;
            }
            else
            {
                var name = action.Method.Name;

                if (name.Contains("<")
                    && name.Contains(">"))
                {
                    // Anonymous method
                    _action = action;
                }
                else
                {
                    Method = action.Method;
                    ActionReference = new WeakReference(action.Target);
                }
            }
            #else
            #if NETFX_CORE
            Method = action.GetMethodInfo();
            #else
            Method = action.Method;
            #endif
            ActionReference = new WeakReference(action.Target);
            #endif

            Reference = new WeakReference(target);
        }
コード例 #6
0
ファイル: WeakAction.cs プロジェクト: LionFree/Cush
        public WeakAction(object target, Action action)
        {
            if (action.GetMethodInfo().IsStatic)
            {
                _staticAction = action;

                if (target != null)
                {
                    // Keep a reference to the target to control the
                    // WeakAction's lifetime.
                    Reference = new WeakReference(target);
                }

                return;
            }

            Method = action.GetMethodInfo();
            ActionReference = new WeakReference(action.Target);
            Reference = new WeakReference(target);
        }
コード例 #7
0
        public static void Run(this ApplicationInstance applicationInstance,
                               Action staticMethodToExecute, 
            TestInjectorSettings testInjectorSettings = null)
        {
            var methodInfo =
            #if NET40
                staticMethodToExecute.Method;
            #else
                staticMethodToExecute.GetMethodInfo();
            #endif
            if (!methodInfo.IsStatic)
                throw new InvalidOperationException("Method must be static - don't reference any outside parameters");

            RunMethodImpl(applicationInstance, methodInfo, testInjectorSettings);
        }
コード例 #8
0
		/// <summary>
		/// Subscribes the event handler.
		/// </summary>
		/// <param name="eventInfo">The event information.</param>
		/// <param name="item">The item.</param>
		/// <param name="action">The action.</param>
		private void AddEventHandler(EventInfo eventInfo, object item, Action action)
		{
			//Got inspiration from here: http://stackoverflow.com/questions/9753366/subscribing-an-action-to-any-event-type-via-reflection
			//Maybe it is possible to pass Event arguments as CommanParameter

			var mi = eventInfo.EventHandlerType.GetRuntimeMethods().First(rtm => rtm.Name == "Invoke");
			List<ParameterExpression> parameters = mi.GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToList();
			MethodInfo actionMethodInfo = action.GetMethodInfo();
			Expression exp = Expression.Call(Expression.Constant(this), actionMethodInfo, null);
			this.handler = Expression.Lambda(eventInfo.EventHandlerType, exp, parameters).Compile();
			eventInfo.AddEventHandler(item, handler);
		}
コード例 #9
0
ファイル: Driver.cs プロジェクト: TerabyteX/main
 private void AddTestCases(IList<TestCase>/*!*/ cases, Action/*!*/ testMethod)
 {
     var attrs = testMethod.GetMethodInfo().GetCustomAttributes<OptionsAttribute>();
     if (attrs.Any()) {
         foreach (OptionsAttribute options in attrs) {
             cases.Add(new TestCase {
                 Name = testMethod.GetMethodInfo().Name,
                 TestMethod = testMethod,
                 Options = options,
             });
         }
     } else {
         cases.Add(new TestCase {
             Name = testMethod.GetMethodInfo().Name,
             TestMethod = testMethod,
             Options = new OptionsAttribute(),
         });
     }
 }
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakAction"/> class.
 /// </summary>
 /// <param name="target">The action's owner.</param>
 /// <param name="action">The action.</param>
 public WeakAction(object target, Action action)
 {
     Method = action.GetMethodInfo();
     ActionReference = new WeakReference(action.Target);
     Reference = new WeakReference(target);
 }