private void AddToContainer(object container, object item)
{
if (container is IList)
{
(container as IList).Add(item);
}
else if (container is ISet)
{
(container as ISet).Add(item);
}
else if (container != null)
{
Type itemType = item.GetType();
Type collectionType = typeof(ICollection<>).MakeGenericType(itemType);
if (collectionType.IsAssignableFrom(container.GetType()))
{
MethodInfo addMethod = container.GetType().GetMethod("Add");
addMethod.Invoke(container, new object[] {item});
}
}
}