下面是相关的代码,有疑问的代码用红色表示。主要是对代码执行的顺序有疑问,所以理解不了。
public sealed class EventHandlerList : IDisposable
{
    // Fields
    private ListEntry head;
    private Component parent;    // Methods
    public EventHandlerList()
    {
    }    internal EventHandlerList(Component parent)
    {
        this.parent = parent;
    }    public void AddHandler(object key, Delegate value)
    {
        ListEntry entry = this.Find(key);
        if (entry != null)
        {
            entry.handler = Delegate.Combine(entry.handler, value);
        }
        else
        {
            this.head = new ListEntry(key, value, this.head);//就是这句
             //既然是给字段head赋值(当然,是把对象引用赋给它),但是,在对象实例化的构造函数中,又使用了head字段。那,岂不是字段head还属于null?  
        }
    }    public void AddHandlers(EventHandlerList listToAddFrom)
    {
        for (ListEntry entry = listToAddFrom.head; entry != null; entry = entry.next)
        {
            this.AddHandler(entry.key, entry.handler);
        }
    }    public void Dispose()
    {
        this.head = null;
    }    private ListEntry Find(object key)
    {
        ListEntry head = this.head;
        while (head != null)
        {
            if (head.key == key)
            {
                return head;
            }
            head = head.next;
        }
        return head;
    }    public void RemoveHandler(object key, Delegate value)
    {
        ListEntry entry = this.Find(key);
        if (entry != null)
        {
            entry.handler = Delegate.Remove(entry.handler, value);
        }
    }    // Properties
    public Delegate this[object key]
    {
        get
        {
            ListEntry entry = null;
            if ((this.parent == null) || this.parent.CanRaiseEventsInternal)
            {
                entry = this.Find(key);
            }
            if (entry != null)
            {
                return entry.handler;
            }
            return null;
        }
        set
        {
            ListEntry entry = this.Find(key);
            if (entry != null)
            {
                entry.handler = value;
            }
            else
            {
                this.head = new ListEntry(key, value, this.head);
            }
        }
    }    // Nested Types
    private sealed class ListEntry
    {
        // Fields
        internal Delegate handler;
        internal object key;
        internal EventHandlerList.ListEntry next;        // Methods
        public ListEntry(object key, Delegate handler, EventHandlerList.ListEntry next)
        {
            this.next = next;
            this.key = key;
            this.handler = handler;
        }
    }

解决方案 »

  1.   

    字段head 还是null。至少从代码看上去是这样的。。不知道是什么功能,所以无法去进行分析
      

  2.   

     该类是实现 自定义event 的+= or -= 当head ==null时
    通过  new ListEntry(key, value, this.head);
    this.head 添加 ListEntry的第一个 节点
    其实我想就是一个简单的链表节点的添加和删除的功能
      

  3.   

    这代码不错,LZ有空能把完整的代码发我么? email:[email protected]
      

  4.   

    this.head = new ListEntry(key, value, this.head);这里,head好像不为空吧,呵呵,只不过它的next为空
    这好像是一个链表
    不知道理解正确不
      

  5.   

    这个和数据结构的栈和队列类似,你在队列中添加一个数据时,next肯定是为空的,然后在添加第二条的时候,会在第一条增加指向第一个的数据信息。