Implementation von Clear bei einer .NET-WinForm-ControlCollection
Frank Dzaebel, erstellt am: 22.11.2005, zuletzt geändert: 22.11.2005
Kategorie:Implementation, .NET-Version:1.1 

Hier wird ein Beispiel für die Framework-Implementierung der Clear()-Methode einer .NET1.1-WinForm-ControlCollection aufgeführt. Es wird deutlich, das kein Dispose stattfindet. Einige zentrale Microsoft-Referenz-Links beschreiben den Hintergrund:
Programmieren für die Garbage Collection
Bereinigen von nicht verwalteten Ressourcen
Finalize-Methoden und Destruktoren

[ListBindable(false),
  DesignerSerializer(@"System.Windows.Forms.Design.ControlCollectionCodeDomSerializer, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
  @"System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class ControlCollection : IList,ICollection,IEnumerable,ICloneable
{
  public virtual void Clear()
  {
    while (this.Count != 0)
    {
      this.RemoveAt(this.Count - 1);
    }
  }

  public void RemoveAt(int index)
  {
    this.Remove(this[index]);
  }

  public virtual void Remove(Control value)
  {
    if ((value != null) && (value.ParentInternal == this.owner))
    {
      value.SetParentHandle(IntPtr.Zero);
      int num1 = this.GetChildIndex(value);
      Array.Copy(this.controls,(int)(num1 + 1),this.controls,num1,(int)((this.controlCount - num1) - 1));
      this.controlCount--;
      this.controls[this.controlCount] = null;
      value.AssignParent(null);
      this.owner.PerformLayout(value,"Parent");
      this.owner.OnControlRemoved(new ControlEventArgs(value));
      ContainerControl control1 = this.owner.GetContainerControlInternal() as ContainerControl;
      if (control1 != null)
      {
        control1.AfterControlRemoved(value);
      }
    }
  }

  internal virtual void AfterControlRemoved(Control control)
  {
    if ((control == this.activeControl) || control.Contains(this.activeControl))
    {
      bool flag1;
      IntSecurity.ModifyFocus.Assert();
      try
      {
        flag1 = base.SelectNextControl(control,true,true,true,true);
      }
      finally
      {
        CodeAccessPermission.RevertAssert();
      }
      if (flag1)
      {
        this.FocusActiveControlInternal();
      }
      else
      {
        this.SetActiveControlInternal(null);
      }
    }
    else if ((this.activeControl == null) && (this.ParentInternal != null))
    {
      ContainerControl control1 = this.ParentInternal.GetContainerControlInternal() as ContainerControl;
      if ((control1 != null) && (control1.ActiveControl == this))
      {
        Form form1 = base.FindFormInternal();
        if (form1 != null)
        {
          IntSecurity.ModifyFocus.Assert();
          try
          {
            form1.SelectNextControl(this,true,true,true,true);
          }
          finally
          {
            CodeAccessPermission.RevertAssert();
          }
        }
      }
    }
    if ((control == this.unvalidatedControl) || control.Contains(this.unvalidatedControl))
    {
      this.unvalidatedControl = null;
    }
  }
}