public static void DelegateTest16() { System.Action a = () => { }; System.Action <int> a_2 = (i) => { }; int a3 = 2; Console.WriteLine(typeof(System.Action) == a.GetType()); //false, should be true Console.WriteLine(typeof(int) == a3.GetType()); //false, should be true }
protected TimeSpan RunAction(Action action, int iterations, string actionName) { actionName = actionName ?? action.GetType().Name; var ticksTaken = Measure(action, iterations); var timeSpan = TimeSpan.FromSeconds(ticksTaken * 1d / Stopwatch.Frequency); Log("{0} took {1}ms ({2} ticks), avg: {3} ticks", actionName, timeSpan.TotalMilliseconds, timeSpan.Ticks, (timeSpan.Ticks / iterations)); return timeSpan; }
public static Delegate Create(EventInfo eventInfo, Action action) { var handlerType = eventInfo.EventHandlerType; var eventParameters = handlerType.GetMethod("Invoke").GetParameters(); var parameters = eventParameters.Select(p => Expression.Parameter(p.ParameterType, "x")); var body = Expression.Call(Expression.Constant(action), action.GetType().GetMethod("Invoke")); var lambda = Expression.Lambda(body, parameters.ToArray()); return Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false); }
public EventPropegator(object instance, EventInfo evt, Action<object, EventArgs> handler) { _instance = instance; _evt = evt; _disposed = false; var methodInfo = handler.GetType().GetMethod("Invoke"); _delegate = Delegate.CreateDelegate(evt.EventHandlerType, handler, methodInfo); evt.AddEventHandler(instance, _delegate); }
public VariableSubscription( VariableSubscriptionToken token, Action<DebugEvaluationResult> executeAction, Action<VariableSubscription> unsubscribeAction) { Token = token; _weakReference = new WeakReference(executeAction.Target); _method = executeAction.Method; _delegateType = executeAction.GetType(); _unsubscribe = unsubscribeAction; }
public static void AddEventHandler(EventInfo eventInfo, object item, Action<object, EventArgs> action) { var parameters = eventInfo.EventHandlerType .GetMethod("Invoke") .GetParameters() .Select(parameter => Expression.Parameter(parameter.ParameterType)) .ToArray(); var invoke = action.GetType().GetMethod("Invoke"); var handler = Expression.Lambda( eventInfo.EventHandlerType, Expression.Call(Expression.Constant(action), invoke, parameters[0], parameters[1]), parameters ) .Compile(); eventInfo.AddEventHandler(item, handler); }
public static void Add(DateTime executeTime, Action<string[]> action, string[] args) { DateTime now = DateTime.Now; if (executeTime < DateTime.Now) { executeTime = now.AddSeconds(1.0); } Maticsoft.TimerTask.Model.TaskTimer timer2 = new Maticsoft.TimerTask.Model.TaskTimer { IsSingle = true, ExecuteType = action.GetType().FullName, ExecuteTime = executeTime }; TimeSpan span = (TimeSpan) (executeTime - now); timer2.Interval = (decimal) span.TotalMilliseconds; timer2.Params = args; Maticsoft.TimerTask.Model.TaskTimer model = timer2; model.ID = Maticsoft.TimerTask.BLL.TaskTimer.Add(model); Instance().CurrentTasks.Add(new Maticsoft.TimerTask.Timer(model, executeTime, action, new Action<Maticsoft.TimerTask.Model.TaskTimer>(Task.CallBack), args)); }
// Accepts a method that does not return a value // Creates a thread to run a function/method private void createThread(Action method) { // Create the thread object, passing in a method Thread aThread = new Thread(new ThreadStart(method)); try { // Add a name for the thread aThread.Name = method.GetType().FullName.ToString(); // Start the thread aThread.Start(); // Wait for thread to start while (!aThread.IsAlive) { // Do nothing, but wait } // Wait for thread to finish aThread.Join(); } catch (System.Net.Mail.SmtpException ex) { // Wait for thread to finish aThread.Join(); MessageBox.Show(ex.Message); } catch (Exception ex) { // Wait for thread to finish aThread.Join(); throw ex; } }