tags:

views:

50

answers:

1

Consider the following class:

public class Event<T>
{
    public delegate void Handler<t>(t msg);
    private event Handler<T> E;

    public void connect(Delegate handler) {
        E += delegate(T msg) {
            object target = handler.Target;

            if (Invokable(target) {
                target.BeginInvoke(handler, new object[] { msg });
            }
        };

    }

    public void emit(T msg) {
        if ( E != null ) {
            E(msg);
        }
    }

    private static bool Invokable(object o) {
                // magic
    }
}

How do I implement Invokable(), and what else do I need for this code to compile? The only other problem that I know of is the target.BeginInvoke call, since target is object.

+2  A: 

If you want to Invoke a System.Windows.Forms.Control

static bool Invokable(object o) {
  bool res = false;
  if(o is System.Windows.Forms.Control) {
   res = ((System.Windows.Forms.Control)o).InvokeRequired;
 }
 return res;
}
chriszero