System.EventArgs 类

方法描述

EventArgs 是包含事件数据的类的基类。

语法定义(C# System.EventArgs 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class EventArgs

构造函数

构造函数名称 构造函数描述
EventArgs 初始化 EventArgs 类的新实例。

成员/方法

方法名称 方法描述
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

此类不包含事件数据,在事件引发时不向事件处理程序传递状态信息的事件会使用此类。 如果事件处理程序需要状态信息,则应用程序必须从此类派生一个类来保存数据。

例如,System.AssemblyLoadEventArgs 类用于保存程序集加载事件的数据,并包含描述所加载程序集的 System.Reflection.Assembly。

有关事件的更多信息,请参见 EventHandler 主题。

System.EventArgs 类例子

在此示例中,FireEventArgs 是从 EventArgs 派生的事件参数集,它在调用 ActivateFireAlarm 引发事件时被传递到 FireEventHandler。

using System;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs: EventArgs {
	public FireEventArgs(string room, int ferocity) {
		this.room = room;
		this.ferocity = ferocity;
	}

	// The fire event will have two pieces of information-- 
	// 1) Where the fire is, and 2) how "ferocious" it is.  

	public string room;
	public int ferocity;

}	//end of class FireEventArgs


// Class with a function that creates the eventargs and initiates the event
public class FireAlarm {

	// Events are handled with delegates, so we must establish a FireEventHandler
	// as a delegate:

	public delegate void FireEventHandler(object sender, FireEventArgs fe);

	// Now, create a public event "FireEvent" whose type is our FireEventHandler delegate. 

	public event FireEventHandler FireEvent;	

	// This will be the starting point of our event-- it will create FireEventArgs,
	// and then raise the event, passing FireEventArgs. 

	public void ActivateFireAlarm(string room, int ferocity) {

		FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

		// Now, raise the event by invoking the delegate. Pass in 
		// the object that initated the event (this) as well as FireEventArgs. 
		// The call must match the signature of FireEventHandler.

		FireEvent(this, fireArgs); 
	}
}	// end of class FireAlarm


// Class which handles the event

class FireHandlerClass {

	// Create a FireAlarm to handle and raise the fire events. 

	public FireHandlerClass(FireAlarm fireAlarm)	{

		// Add a delegate containing the ExtinguishFire function to the class'
		// event so that when FireAlarm is raised, it will subsequently execute 
		// ExtinguishFire.

		fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
	}

	// This is the function to be executed when a fire event is raised. 

	void ExtinguishFire(object sender, FireEventArgs fe) {

		Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());

		// Now, act in response to the event.

		if (fe.ferocity < 2)
			Console.WriteLine("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room);
		else if (fe.ferocity < 5)
			Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.",  fe.room);
		else 
			Console.WriteLine("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room);
	}
}	//end of class FireHandlerClass

public class FireEventTest {
	public static void Main () 	{	

		// Create an instance of the class that will be firing an event.

		FireAlarm myFireAlarm = new FireAlarm();
		
		// Create an instance of the class that will be handling the event. Note that 
		// it receives the class that will fire the event as a parameter. 

		FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);
		
		//use our class to raise a few events and watch them get handled
		myFireAlarm.ActivateFireAlarm("Kitchen", 3);
		myFireAlarm.ActivateFireAlarm("Study", 1);
		myFireAlarm.ActivateFireAlarm("Porch", 5);
		
		return;

	}	//end of main

}	// end of FireEventTest

继承层次结构

System.Object

System.EventArgs

更多...

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。

相关资源

System 命名空间
MSDN