예제 #1
0
        public virtual void AddToCollection(object instance, object item)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            IList list = instance as IList;

            if (list != null)
            {
                list.Add(item);
                return;
            }

            ThrowIfUnknown();
            if (!_xamlType.IsCollection)
            {
                throw new NotSupportedException(SR.Get(SRID.OnlySupportedOnCollections));
            }
            XamlType itemType;

            if (item != null)
            {
                itemType = _xamlType.SchemaContext.GetXamlType(item.GetType());
            }
            else
            {
                itemType = _xamlType.ItemType;
            }
            MethodInfo addMethod = GetAddMethod(itemType);

            if (addMethod == null)
            {
                throw new XamlSchemaException(SR.Get(SRID.NoAddMethodFound, _xamlType, itemType));
            }
            SafeReflectionInvoker.InvokeMethod(addMethod, instance, new object[] { item });
        }
예제 #2
0
        public virtual void AddToDictionary(object instance, object key, object item)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            IDictionary dictionary = instance as IDictionary;

            if (dictionary != null)
            {
                dictionary.Add(key, item);
                return;
            }

            ThrowIfUnknown();
            if (!_xamlType.IsDictionary)
            {
                throw new NotSupportedException(SR.Get(SRID.OnlySupportedOnDictionaries));
            }
            XamlType itemType;

            if (item != null)
            {
                itemType = _xamlType.SchemaContext.GetXamlType(item.GetType());
            }
            else
            {
                itemType = _xamlType.ItemType;
            }
            MethodInfo addMethod = GetAddMethod(itemType);

            if (addMethod == null)
            {
                throw new XamlSchemaException(SR.Get(SRID.NoAddMethodFound, _xamlType, itemType));
            }
            SafeReflectionInvoker.InvokeMethod(addMethod, instance, new object[] { key, item });
        }
예제 #3
0
 private object CreateInstanceWithActivator(Type type, object[] arguments)
 {
     return(SafeReflectionInvoker.CreateInstance(type, arguments));
 }