, . , , , ' . , '. г , - ' . Add Remove 䳿. 䳿, . :

public event <' > <' 䳿>
{
add {...}
remove {...}
}

, . ', - .

. , :

class ClassWithManyEvents
{
//
Hashtable DStore = new Hashtable ();

// 䳿:
public event EventHandler Ev1
{
add {DStore [ "Ev1"] = (EventHandler) DStore [ "Ev1"] + value; }
remove {DStore [ "Ev1"] = (EventHandler) DStore [ "Ev1"] - value; }
}
public event EventHandler Ev2
{
add {DStore [ "Ev2"] = (EventHandler) DStore [ "Ev2"] + value; }
remove {DStore [ "Ev2"] = (EventHandler) DStore [ "Ev2"] - value; }
}
public event EventHandler Ev3
{
add {DStore [ "Ev3"] = (EventHandler) DStore [ "Ev3"] + value; }
remove {DStore [ "Ev3"] = (EventHandler) DStore [ "Ev3"] - value; }
}
public event EventHandler Ev4
{
add {DStore [ "Ev4"] = (EventHandler) DStore [ "Ev4"] + value; }
remove {DStore [ "Ev4"] = (EventHandler) DStore [ "Ev4"] - value; }
}

// ,
public void SimulateEvs ()
{
EventHandler ev = (EventHandler) DStore [ "Ev1"];
if (ev! = null) ev (this, null);
ev = (EventHandler) DStore [ "Ev3"];
if (ev! = null) ev (this, null);
}
}

䳿 - DStore . 䳿 EventHandler. 䳿 , add, -. , , ' 䳿. ' remove, -. SimulateEvs, - Ev1 Ev3.

ReceiverEvs, 䳿. , 䳿; , ' ; 䳿 - , OnConnect, ' . :

class ReceiverEvs
{
private ClassWithManyEvents manyEvs; // , 䳿
public ReceiverEvs (ClassWithManyEvents manyEvs) //
{
this.manyEvs = manyEvs;
OnConnect (); // ϳ 䳿
}

// ( "") 䳿
public void OnConnect ()
{
lassWithmanyEvs.Ev1 + = new EventHandler (H1);
lassWithmanyEvs.Ev2 + = new EventHandler (H2);
lassWithmanyEvs.Ev3 + = new EventHandler (H3);
ClassWithmanyEvs.Ev4 + = new EventHandler (H4);
}

// 䳿:
public void H1 (object s, EventArgs e) {Console.WriteLine ( " Ev1"); }
public void H2 (object s, EventArgs e) {Console.WriteLine ( " Ev2"); }
public void H3 (object s, EventArgs e) {Console.WriteLine ( " Ev3"); }
public void H4 (object s, EventArgs e) {Console.WriteLine ( " Ev4"); }
}

, ' Simulate, 䳿:

public void TestManyEvents ()
{
ClassWithManyEvents me = new ClassWithManyEvents ();
ReceiverEvs revs = new ReceiverEvs (me);
me.SimulateEvs ();
}

.



.NET | ' 䳿,

䳿. | .NET. | 䳿 | 䳿 | 䳿. | |

© um.co.ua -