If you want to reprint, please retain my copyright:
/*
*description:asp. NET custom server controls internal details series tutorials
*auther: Sung-Sung--innocent, good blue.
*msn:chongchong2008@msn.com
*dates:2007-05-20
*copyright:chongchong2008 Yichang Hubei
*/
About customizing ASP. NET custom server control articles and source code examples online has been a lot, but some of the internal section, so that some of the first contact with the technology of friends difficult to understand, the server control aspects are also confused. To this end, I take some time to put the things I understand to organize, to facilitate the exchange of learning.
An understanding of event delegates, events, raising events, and event optimization
. NET Framework's naming convention for event delegates and event data:
Event data classes consist of event names plus suffix EventArgs, such as: Sxlogineventargs
Event delegates are composed of event names plus suffix EventHandler, such as: Sxlogineventhandler
The name of the method that raises the event is preceded by a prefix to the event On,onsxlogin
1. Event delegates
A delegate is actually a class, the delegate has the granularity of function pointers, and the security of the interface, why, because a delegate is a function pointer of a security type that is used to callback a method, and the signature of the method must match the signature of the delegate.
Here we look at the Commission's reputation:
public delegate void Sxlogineventhandler (Object Sender,sxlogineventargs e);
The above definition indicates that a Sxlogineventhandler event delegate is defined, the return type is void, and an argument of object and Sxlogineventargs type is accepted respectively. object represents the sender of the event, and E represents the data that describes the event.
It can be seen that the reputation of the event delegation and the name of the class are similar, just add the keyword delegate.
The delegate indirectly derives from the System.Delegate and derives directly from the System.MulticastDelegate.
2. Event
An event is a message or notification sent by a class when an action occurs or a state changes.
Let's take a look at the event's reputation:
Public event Sxlogineventhandler Sxlogin, you can see that the event is associated with a delegate.
3. Raising an Event
To implement events in a class, you need an event data class, an event delegate, and a method to publish event notifications. We need to put these together, and here's an example:
3.1 If the class does not have any associated event data, use the EventArgs class directly, or other existing event data classes that must match. Otherwise, you need to define an event data class that must derive from System.EventArgs, as follows:
public class sxlogineventargs:eventargs{...}
3.2 If the event has no associated data, use System.EventHandler directly as the event delegate, otherwise you will need to set an event delegate as follows:
public delegate void Sxlogineventhandler (object sender, Sxlogineventargs e);
3.3 Defines an event member with the event keyword, followed by the corresponding event delegate, as follows:
public event Sxlogineventhandler Sxlogin;
3.4 Define a virtual method invocation event delegate in the class, with the name of the method preceded by a prefix on the event, as follows:
protected virtual void Onsxlogin (Sxlogineventargs e)
{
if (Sxlogin!= null)
{
Sxlogin (this, e);
}
}
4. Event optimization
If more than one event is raised in a class, it is inefficient to do so by reputation of an event field member for each event, so we can use attributes to define the event in a different way.
. NET Framework has a System.ComponentModel.EventHanlderList class, which is an optimized list of delegate storage and retrieval. Here's how the optimization event pattern is implemented:
Private Eventhanlderlist events;
Protected Eventhanlderlist Events
{
Get
{
if (events==null)
{
Events = new Eventhanlderlist ();
}
return events;
}
}
Protected static ReadOnly Object sxlogineventobject = new Object ();
We use attributes instead of fields to define events, as follows:
Public event Sxlogineventhandler Sxlogin
{
Add{events.addhandler (Sxlogineventobject,value)};
Remove{events.removehandler (Sxlogineventobject,value)};
}