C#インタフェースイベントテスト

6692 ワード


  
    
using System;
using System.Collections.Generic;
using System.Text;

namespace ImplementInterfaceEvents
{
interface IDrawingObject
{
event EventHandler ShapeChanged;
}

public class MyEventArgs : EventArgs
{
private string dispalyMsg;

public string DispalyMsg
{
get { return dispalyMsg; }
}

public MyEventArgs()
:
this ( string .Empty)
{
}

public MyEventArgs( string msg)
{
this .dispalyMsg = msg;
}
}

public class Shape : IDrawingObject
{
public event EventHandler ShapeChanged;

public void ChangeShape()
{
Console.WriteLine(
" Do something here before the event… " );
OnShapeChanged(
new MyEventArgs( " hello " ));
Console.WriteLine(
" or do something here after the event . " );
}

protected virtual void OnShapeChanged(MyEventArgs e)
{
if (ShapeChanged != null )
{
ShapeChanged(
this , e);
}
}

}

public class TestDemo
{
static void Main( string [] args)
{

Shape s
= new Shape();
s.ShapeChanged
+= new EventHandler(s_ShapeChanged);
s.ChangeShape();
Console.ReadLine();
}

static void s_ShapeChanged( object sender, EventArgs e)
{
Console.WriteLine(((MyEventArgs)e).DispalyMsg);
Console.WriteLine(
" s_ShapeChanged " );
}

}
}