如何跨对象链处理事件?

时间:2012-09-03 07:54:06

标签: c# .net events event-handling

如果有以下内容:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.Method1();
}
public class MyClass1() {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1();
    }
}
public class MyClass2() {
   public void Method1() {
       MyClass3 obj = new MyClass3();
       obj.Method1();
   }
}
public class MyClass3() {
   public void Method1() {
       // Raise event here that is handled in MyClass1?    
   }
}

可以MyClass3.Method1()举起MyClass1处理的事件吗?

如果我想实现这一点,如何编写事件处理代码?

5 个答案:

答案 0 :(得分:4)

是的,但是因为每个级别都不知道你链的更深层次,所以你必须在每个类上创建事件。有人喜欢这样:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.MyEvent += (s, e) => { Console.WriteLine("Fired!"); };
    obj.Method1();
}

public class MyClass1 {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.MyEvent += (s, e) => { OnMyEvent(); };
        obj.Method1();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}
public class MyClass2 {
    public void Method1() {
        MyClass3 obj = new MyClass3();
        obj.MyEvent += (s, e) => { OnMyEvent(); };
        obj.Method1();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}
public class MyClass3 {
    public void Method1() {
        // Raise event here that is handled in MyClass1?    
        OnMyEvent();
    }
    public event EventHandler MyEvent;
    private void OnMyEvent() {
        var myEvent = MyEvent;
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}

答案 1 :(得分:2)

要链接事件处理程序,请使用MyClass2中的添加/删除语法。从MyClass1,设置SomeEvent,在MyClass3中,将其抬起。

public class MyClass1             
{             
    MyClass2 obj = new MyClass2(); 

    public MyClass1()
    {
        obj.SomeEvent += obj_SomeEvent;
    }

    public void Method1()             
    {                      
        obj.Method1();             
    }             

    private static void obj_SomeEvent(object sender, EventArgs e)             
    {             
        Console.WriteLine("Some event fired");             
    }             
}  


public class MyClass2() 
{    
   MyClass3 cls3 = new MyClass3();

   public void Method1() 
   {     
       cls3.FireSomeEvent();    
   }   

    public event MyEventHandler SomeEvent
    { 
        add { this.cls3.SomeEvent += value; } 
        remove { this.cls3.SomeEvent -= value; } 
    }  
}

public class MyClass3() 
{
    public event EventHandler SomeEvent;

    private void OnSomeEvent() 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(this, new EventArgs()); 
        } 
    } 

    public void FireSomeEvent
    {
        OnSomeEvent();
    }
}

答案 2 :(得分:1)

事件处理ABC假设您拥有订阅者和发布者。因此,您可能希望MyClass3具有公共事件,而MyClass1则订阅此事件。

但是在您的特定代码中,这种复杂性没有任何意义 - 只是使用回调函数的最简单方法:

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.Method1();
}
public class MyClass1{
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1(MyEventHandler);
    }

    public void MyEventHandler() {
    //...
    }

}
public class MyClass2{
   public void Method1(Action callback) {
       MyClass3 obj = new MyClass3();
       obj.Method1(callback);
   }
}
public class MyClass3{
   public void Method1(Action callback) {
       // Raise event here that is handled in MyClass1?    
       callback();
   }
}

答案 3 :(得分:1)

您可以将事件添加到中间类,以便进行连接。像这样:

using System;
using System.Windows.Forms;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass1 obj = new MyClass1();
            obj.Method1();
        }
    }

    public class MyClass1
    {
        public void Method1()
        {
            MyClass2 obj = new MyClass2();
            obj.SomethingHappened += somethingHappened;
            obj.Method1();
        }

        private static void somethingHappened(object sender, EventArgs e)
        {
            Console.WriteLine("Something happened!");
        }
    }

    public class MyClass2
    {
        public void Method1()
        {
            MyClass3 obj = new MyClass3();
            obj.SomethingHappened += onSomethingHappened;
            obj.Method1();
        }

        public event EventHandler SomethingHappened;

        private void onSomethingHappened(object sender, EventArgs e)
        {
            var handler = SomethingHappened;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

    public class MyClass3
    {
        public void Method1()
        {
            onSomethingHappened();
        }

        private void onSomethingHappened()
        {
            var handler = SomethingHappened;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        public event EventHandler SomethingHappened;
    }
}

您可能想要考虑的一件事是您使用中间类中的“sender”参数。您可以将其设为MyClass2(如上面的代码所示),或者您可以像这样保留原始发件人:

private void onSomethingHappened(object sender, EventArgs e)
{
    var handler = SomethingHappened;

    if (handler != null)
    {
        handler(sender, e);
    }
}

答案 4 :(得分:1)

如果你想避免每个班级的回调解决方案和事件链,你基本上有2个解决方案。

第一个包括将MyClassX类型的局部变量转换为字段,即像Chris Gessler建议的那样,但完全遵循这种方法并删除本地变量。

public static void main() { 
    MyClass1 obj = new MyClass1();
    obj.c2.c3.SomeEvent += obj_SomeEvent;      
    obj.Method1();
}

private static void obj_SomeEvent(object sender, EventArgs e)             
{             
    Console.WriteLine("Some event fired");             
}

public class MyClass1() {
    public MyClass2 c2 = new MyClass2();

    public void Method1() {
        c2.Method1();
    }
}
public class MyClass2() {
   public MyClass3 c3 = new MyClass3();

   public void Method1() {
       c3.Method1();
   }
}
public class MyClass3() {
    public event EventHandler SomeEvent;

    private void OnSomeEvent() 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(this, new EventArgs()); 
        } 
    } 
   public void Method1() {
       OnSomeEvent();    
   }
}

你的另一个选择(但它真的取决于你试图做什么,如果可行的话,我不喜欢)就是简单地将MyClass3中的事件定义为静态:

public static void main() { 
    MyClass3.SomeEvent += obj_SomeEvent;
    MyClass1 obj = new MyClass1();
    obj.Method1();
}

private static void obj_SomeEvent(object sender, EventArgs e)             
{             
     Console.WriteLine("Some event fired");             
}

public class MyClass1() {
    public void Method1() {
        MyClass2 obj = new MyClass2();
        obj.Method1();
    }
}
public class MyClass2() {
   public void Method1() {
       MyClass3 obj = new MyClass3();
       obj.Method1();
   }
}
public class MyClass3() {
    public static event EventHandler SomeEvent;

    private void OnSomeEvent(MyClass3 anObj) 
    { 
        if (SomeEvent!= null) 
        { 
            SomeEvent(anObj, new EventArgs()); 
        } 
    }

    public void Method1() {
       OnSomeEvent(this);    
    }
}