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); } }
/// <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 }); }
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; }
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(); }
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); }
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); }
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); }
/// <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); }
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(), }); } }
/// <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); }