public virtual void AddToCollection (object instance, object item)
{
if (instance == null)
throw new ArgumentNullException ("instance");
if (item == null)
throw new ArgumentNullException ("item");
var ct = instance.GetType ();
var xct = type == null ? null : type.SchemaContext.GetXamlType (ct);
MethodInfo mi = null;
if (type != null && type.UnderlyingType != null) {
if (!xct.IsCollection) // not sure why this check is done only when UnderlyingType exists...
throw new NotSupportedException (String.Format ("Non-collection type '{0}' does not support this operation", xct));
if (ct.IsAssignableFrom (type.UnderlyingType))
mi = GetAddMethod (type.SchemaContext.GetXamlType (item.GetType ()));
}
if (mi == null) {
if (ct.IsGenericType)
mi = ct.GetMethod ("Add", ct.GetGenericArguments ());
else
mi = ct.GetMethod ("Add", new Type [] {typeof (object)});
}
if (mi == null)
throw new InvalidOperationException (String.Format ("The collection type '{0}' does not have 'Add' method", ct));
mi.Invoke (instance, new object [] {item});
}