context: using System;
  using System.Collections;
  #if VSCREAM
  using System.Drawing;
  #endif // VSCREAM
  
  namespace Cream
  {
      /// <summary>
      /// This interface should be implemented by any class whose instances are intended 
      /// to be executed by a thread.
      /// </summary>
      public interface IThreadRunnable
      {
          /// <summary>
          /// This method has to be implemented in order that starting of the thread causes the object's 
          /// run method to be called in that separately executing thread.
          /// </summary>
          void Run();
      }
  
      /// <summary>
      /// Contains conversion support Elements such as classes, interfaces and static methods.
      /// </summary>
      public class SupportClass
      {
          /// <summary>
          /// This class provides functionality not found in .NET collection-related interfaces.
          /// </summary>
          public class CollectionSupport
          {
              /// <summary>
              /// Adds a new Element to the specified collection.
              /// </summary>
              /// <param name="c">Collection where the new Element will be added.</param>
              /// <param name="obj">Object to Add.</param>
              /// <returns>true</returns>
              public static bool Add(ICollection c, Object obj)
              {
                  bool added = false;
                  //Reflection. Invoke either the "Add" or "Add" method.
                  System.Reflection.MethodInfo method;
                  try
                  {
                      //Get the "Add" method for proprietary classes
                      method = c.GetType().GetMethod("Add") ?? c.GetType().GetMethod("Add");
                      var index = (int) method.Invoke(c, new[] {obj});
                      if (index >=0)	
                          added = true;
                  }
                  catch (Exception e)
                  {
                      throw e;
                  }
                  return added;
              }
  
              /// <summary>
              /// Adds all of the Elements of the "c" collection to the "target" collection.
              /// </summary>
              /// <param name="target">Collection where the new Elements will be added.</param>
              /// <param name="c">Collection whose Elements will be added.</param>
              /// <returns>Returns true if at least one Element was added, false otherwise.</returns>
              public static bool AddAll(ICollection target, ICollection c)
              {
                  IEnumerator e = new ArrayList(c).GetEnumerator();
                  bool added = false;
  
                  //Reflection. Invoke "addAll" method for proprietary classes
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = target.GetType().GetMethod("addAll");
  
                      if (method != null)
                          added = (bool) method.Invoke(target, new Object[] {c});
                      else
                      {
                          method = target.GetType().GetMethod("Add");
                          while (e.MoveNext())
                          {
                              bool tempBAdded =  (int) method.Invoke(target, new[] {e.Current}) >= 0;
                              added = added ? true : tempBAdded;
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
                  return added;
              }
  
              /// <summary>
              /// Removes all the Elements from the collection.
              /// </summary>
              /// <param name="c">The collection to Remove Elements.</param>
              public static void Clear(ICollection c)
              {
                  //Reflection. Invoke "Clear" method or "clear" method for proprietary classes
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = c.GetType().GetMethod("Clear") ?? c.GetType().GetMethod("clear");
  
                      method.Invoke(c, new Object[] {});
                  }
                  catch (Exception e)
                  {
                      throw e;
                  }
              }
  
              /// <summary>
              /// Determines whether the collection Contains the specified Element.
              /// </summary>
              /// <param name="c">The collection to check.</param>
              /// <param name="obj">The object to locate in the collection.</param>
              /// <returns>true if the Element is in the collection.</returns>
              public static bool Contains(ICollection c, Object obj)
              {
                  bool contains;
  
                  //Reflection. Invoke "Contains" method for proprietary classes
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = c.GetType().GetMethod("Contains") ?? c.GetType().GetMethod("Contains");
  
                      contains = (bool)method.Invoke(c, new[] {obj});
                  }
                  catch (Exception e)
                  {
                      throw e;
                  }
  
                  return contains;
              }
  
              /// <summary>
              /// Determines whether the collection Contains all the Elements in the specified collection.
              /// </summary>
              /// <param name="target">The collection to check.</param>
              /// <param name="c">Collection whose Elements would be checked for containment.</param>
              /// <returns>true id the target collection Contains all the Elements of the specified collection.</returns>
              public static bool ContainsAll(ICollection target, ICollection c)
              {						
                  IEnumerator e =  c.GetEnumerator();
  
                  bool contains = false;
  
                  //Reflection. Invoke "containsAll" method for proprietary classes or "Contains" method for each Element in the collection
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = target.GetType().GetMethod("containsAll");
  
                      if (method != null)
                          contains = (bool)method.Invoke(target, new Object[] {c});
                      else
                      {					
                          method = target.GetType().GetMethod("Contains");
                          while (e.MoveNext())
                          {
                              if ((contains = (bool)method.Invoke(target, new[] {e.Current})) == false)
                                  break;
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
  
                  return contains;
              }
  
              /// <summary>
              /// Removes the specified Element from the collection.
              /// </summary>
              /// <param name="c">The collection where the Element will be removed.</param>
              /// <param name="obj">The Element to Remove from the collection.</param>
              public static bool Remove(ICollection c, Object obj)
              {
                  bool changed = false;
  
                  //Reflection. Invoke "Remove" method for proprietary classes or "Remove" method
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = c.GetType().GetMethod("Remove");
  
                      if (method != null)
                          method.Invoke(c, new[] {obj});
                      else
                      {
                          method = c.GetType().GetMethod("Contains");
                          changed = (bool)method.Invoke(c, new[] {obj});
                          method = c.GetType().GetMethod("Remove");
                          method.Invoke(c, new[] {obj});
                      }
                  }
                  catch (Exception e)
                  {
                      throw e;
                  }
  
                  return changed;
              }
  
              /// <summary>
              /// Removes all the Elements from the specified collection that are contained in the target collection.
              /// </summary>
              /// <param name="target">Collection where the Elements will be removed.</param>
              /// <param name="c">Elements to Remove from the target collection.</param>
              /// <returns>true</returns>
              public static bool RemoveAll(ICollection target, ICollection c)
              {
                  ArrayList al = ToArrayList(c);
                  IEnumerator e = al.GetEnumerator();
  
                  //Reflection. Invoke "removeAll" method for proprietary classes or "Remove" for each Element in the collection
                  System.Reflection.MethodInfo method;
                  try
                  {
                      method = target.GetType().GetMethod("removeAll");
  
                      if (method != null)
                          method.Invoke(target, new Object[] {al});
                      else
                      {
                          method = target.GetType().GetMethod("Remove");
                          System.Reflection.MethodInfo methodContains = target.GetType().GetMethod("Contains");
  
                          while (e.MoveNext())
                          {
                              while ((bool) methodContains.Invoke(target, new[] {e.Current}))
                                  method.Invoke(target, new[] {e.Current});
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
                  return true;
              }
  
              /// <summary>
              /// Retains the Elements in the target collection that are contained in the specified collection
              /// </summary>
              /// <param name="target">Collection where the Elements will be removed.</param>
              /// <param name="c">Elements to be retained in the target collection.</param>
              /// <returns>true</returns>
              public static bool RetainAll(ICollection target, ICollection c)
              {
                  IEnumerator e = new ArrayList(target).GetEnumerator();
                  var al = new ArrayList(c);
  
                  //Reflection. Invoke "retainAll" method for proprietary classes or "Remove" for each Element in the collection
                  System.Reflection.MethodInfo method;
                  try
                  {
  
gt:                     method = c.GetType().GetMethod("retainAll");