Event proxy often makes programmers feel a headache. After reading this example, you may be panic.
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace eventdelegateapp
{
// The deletegate function with parameters is: the old version knows who is going to play games at work. As long as an employee plays games, immediately notify the boss via proxy
Public Delegate void deletegateclasshandle (Object sender, customereventargs E );
Class Program
{
Static void main (string [] ARGs)
{
Employee Employee = new employee ();
Employee. Age = 18;
Employee. Name = "Mike ";
Admin admin = new admin ();
Employee. playgame + = new deletegateclasshandle (Admin. Policy );
Employee. Games ();
Console. Read ();
}
}
Public class customereventargs: eventargs
{
String name = "";
Int age = 0;
Public customereventargs ()
{}
Public string name
{
Get {return this. Name ;}
Set {This. Name = value ;}
}
Public int age
{
Get {return this. Age ;}
Set {This. Age = value ;}
}
}
Public class employee
{
Private string _ name;
Private int _ age;
Public string name
{
Get {return this. _ name ;}
Set {This. _ name = value ;}
}
Public int age
{
Get {return this. _ age ;}
Set {This. _ age = value ;}
}
Public event deletegateclasshandle playgame;
Public void games ()
{
If (playgame! = NULL)
{
Customereventargs EE = new customereventargs ();
Ee. Age = This. _ age;
Ee. Name = This. _ name;
Playgame (this, ee );
}
}
}
Public class Admin
{
Public void Policy (Object sender, customereventargs E)
{
Console. writeline (E. Age. tostring () + "years old" + E. Name + "is playing game ");
}
}
}