Removes all the elements from the target ICollection that are contained in the source collection. - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Removes all the elements from the target ICollection that are contained in the source collection.

Demo Code

/*//from  w w w.j a va2 s.  c o m
 * Copyright ? 2002-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.Reflection;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Removes all the elements from the target collection that are contained in the source collection.
        /// </summary>
        /// <param name="targetCollection">Collection where the elements will be removed.</param>
        /// <param name="sourceCollection">Elements to remove from the target collection.</param>
        public static void RemoveAll(ICollection targetCollection, ICollection sourceCollection)
        {
            if (targetCollection == null || sourceCollection == null)
            {
                throw new ArgumentNullException("Collection cannot be null.");
            }
            ArrayList al = ToArrayList(sourceCollection);

            MethodInfo method;
            method = targetCollection.GetType().GetMethod("removeAll", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);

            if (method != null)
                method.Invoke(targetCollection, new Object[] { al });
            else
            {
                method = targetCollection.GetType().GetMethod("Remove", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, new Type[1] { typeof(object) }, null);
                MethodInfo methodContains = targetCollection.GetType().GetMethod("Contains", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
                if (method == null)
                {
                    throw new InvalidOperationException("Target Collection must implement either a RemoveAll() or Remove() method.");
                }
                if (methodContains == null)
                {
                    throw new InvalidOperationException("TargetCollection must implement a Contains() method.");
                }
                IEnumerator e = al.GetEnumerator();
                while (e.MoveNext() == true)
                {
                    while ((bool)methodContains.Invoke(targetCollection, new Object[] { e.Current }) == true)
                        method.Invoke(targetCollection, new Object[] { e.Current });
                }
            }
        }
}

Related Tutorials