System.Xaml.Schema.XamlTypeInvoker.AddToCollection C# (CSharp) Méthode

AddToCollection() public méthode

public AddToCollection ( object instance, object item ) : void
instance object
item object
Résultat void
		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});
		}

Usage Example

Exemple #1
0
		public void AddToCollectionTypeMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			var l = new List<int> ();
			i.AddToCollection (l, "5");
		}
All Usage Examples Of System.Xaml.Schema.XamlTypeInvoker::AddToCollection