开发者

How do I find out if an object can be Invoke()'d?

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 im开发者_如何学编程plement 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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜