//When you design an event you have to do 3 steps
//1. Declare a private member for the event
//private event EventHandler<MyEventType> _EventName;
//2. Declare public accessors for adding and removing handlers
//public event EventHandler<MyEventType> EventName
//{
// add { _EventName += value; }
// remove { _EventName -= value; }
//}
//3. Provide a virtual OnEventName method
//virtual void OnEventName(MyEventType e)
//{
// if(_EventName != null)
// {
// try
// {
// _EventName(this,e);
// }
// catch(Exception e)
// {
// //your code here
// }
// }
//}
using System;
namespace Lab
{
interface IEventNum
{
int EventNum
{
get; set;
}
}
//Define an interface that can perform operations on a generic type parameter
interface ITEventNum<T>
{
T EventNum
{
get;
set;
}
}
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
public class MyEventArgs1 : EventArgs, IEventNum
{
public int _EventNum;
public int EventNum
{
get { return _EventNum; }
set { _EventNum = value; }
}
}
//Create a delegate signature for the event.
public delegate void MyEventHandler(object source, MyEventArgs1 arg);
class MyEvent1
{
private static int count = 0;
// Declare an event of delegate type MyEventHandler of MyEventArgs1.
// Implicit Registration
public event MyEventHandler SomeEvent;
//Create a method on the type that will raise the event.
public virtual void OnSomeEvent()
{
MyEventArgs1 arg = new MyEventArgs1();
// Copy to a temporary variable to be thread-safe.
MyEventHandler temp = SomeEvent;
if (SomeEvent != null)
{
try
{
arg.EventNum = count++;
temp(this, arg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
public class MyEventArgs2 : EventArgs, IEventNum
{
public int _EventNum;
public int EventNum
{
get { return _EventNum; }
set { _EventNum = value; }
}
}
class MyEvent2
{
private static int count = 0;
//Generic EventHandler<T> delegate to declare an event bound to the specific EventArgs derivative
//Implicit Registration
public event EventHandler<MyEventArgs2> SomeEvent;
//Create a method on the type that will raise the event.
public virtual void OnSomeEvent()
{
MyEventArgs2 arg = new MyEventArgs2();
// Copy to a temporary variable to be thread-safe.
EventHandler<MyEventArgs2> temp = SomeEvent;
if (temp != null)
{
try
{
arg.EventNum = count++;
temp(this, arg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
//When you implement a generic interface, the supporting type specifies the placeholder type
public class MyEventArgs3<T> : EventArgs, ITEventNum<T>
{
public T _EventNum;
public T EventNum
{
get { return _EventNum; }
set { _EventNum = value; }
}
}
class MyEvent3
{
private static int count = 0;
// Declare an event of delegate type EventHandler of MyEventArgs3.
// Implicit Registration
// public event EventHandler<MyEventArgs3<int>> SomeEvent;
// Explicit registration
private EventHandler<MyEventArgs3<int>> _SomeEvent;
public event EventHandler<MyEventArgs3<int>> SomeEvent
{
add { _SomeEvent += value; }
remove { _SomeEvent -= value; }
}
//Create a method on the type that will raise the event.
public virtual void OnSomeEvent()
{
MyEventArgs3<int> arg = new MyEventArgs3<int>();
// Copy to a temporary variable to be thread-safe.
EventHandler<MyEventArgs3<int>> temp = _SomeEvent;
if (temp != null)
{
try
{
arg.EventNum = count++;
temp(this, arg);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
class EventDemo1 : MyEvent1
{
public void TestMain()
{
Test1 test1 = new Test1();
MyEvent1 evt;
evt = new EventDemo1();
MyEventArgs1 arg = new MyEventArgs1();
// Add handler() to the event list.
evt.SomeEvent += new MyEventHandler(test1.handler);
// Fire the event.
evt.OnSomeEvent();
evt.OnSomeEvent();
}
public override void OnSomeEvent()
{
base.OnSomeEvent();
Console.WriteLine("Override");
}
}
class EventDemo2 : MyEvent2
{
public void TestMain()
{
Test2 test2 = new Test2();
MyEvent2 evt;
evt = new EventDemo2();
MyEventArgs2 arg = new MyEventArgs2();
// Add Handler() to the event list.
evt.SomeEvent += new EventHandler<MyEventArgs2>(test2.Handler);
// Fire the event.
evt.OnSomeEvent();
evt.OnSomeEvent();
}
public override void OnSomeEvent()
{
base.OnSomeEvent();
Console.WriteLine("Override");
}
}
class EventDemo3 : MyEvent3
{
public void TestMain()
{
Test3 test3 = new Test3();
MyEvent3 evt;
evt = new EventDemo3();
MyEventArgs3<int> arg = new MyEventArgs3<int>();
// Add Handler() to the event list.
evt.SomeEvent += new EventHandler<MyEventArgs3<int>>(test3.Handler);
// Fire the event.
evt.OnSomeEvent();
evt.OnSomeEvent();
}
public override void OnSomeEvent()
{
base.OnSomeEvent();
Console.WriteLine("Override");
}
}
class Test1
{
public void handler(object source, MyEventArgs1 arg)
{
if (source == null) throw new ArgumentNullException("source");
Result.Write(source, arg);
}
}
class Test2
{
public void Handler(object source, MyEventArgs2 arg)
{
if (source == null) throw new ArgumentNullException("source");
Result.Write(source, arg);
}
}
class Test3
{
public void Handler(object source, MyEventArgs3<int> arg)
{
if (source == null) throw new ArgumentNullException("source");
Result.Write(source, arg);
}
}
class Result
{
public static void Write(object source, IEventNum arg)
{
Console.WriteLine("Event {0} received by an Test object.", arg.EventNum);
Console.WriteLine("Source is " + source);
}
public static void Write(object source, ITEventNum<int> arg)
{
Console.WriteLine("Event {0} received by an Test object.", arg.EventNum);
Console.WriteLine("Source is " + source);
}
}
class program
{
public static void Main()
{
EventDemo1 evt1 = new EventDemo1();
evt1.TestMain();
EventDemo2 evt2 = new EventDemo2();
evt2.TestMain();
EventDemo3 evt3 = new EventDemo3();
evt3.TestMain();
Console.Read();
}
}
}
No comments:
Post a Comment