Exemplo n.º 1
0
        /// <summary>
        /// Return the object associated with (collection, cvs, type).
        /// If this is the first reference to this view, add it to the tables.
        /// </summary>
        /// <exception cref="ArgumentException">
        /// Thrown when the collectionViewType does not implement ICollectionView
        /// or does not have a constructor that accepts the type of collection.
        /// Also thrown when the named collection view already exists and is
        /// not the specified collectionViewType.
        /// </exception>
        internal ViewRecord GetViewRecord(object collection, CollectionViewSource cvs, Type collectionViewType, bool createView, Func <object, object> GetSourceItem)
        {
            // Order of precendence in acquiring the View:
            // 0) If  collection is already a CollectionView, return it.
            // 1) If the CollectionView for this collection has been cached, then
            //    return the cached instance.
            // 2) If a CollectionView derived type has been passed in collectionViewType
            //    create an instance of that Type
            // 3) If the collection is an ICollectionViewFactory use ICVF.CreateView()
            //    from the collection
            // 4) If the collection is an IListSource call GetList() and perform 5),
            //    etc. on the returned list
            // 5) If the collection is an IBindingList return a new BindingListCollectionView
            // 6) If the collection is an IList return a new ListCollectionView
            // 7) If the collection is an IEnumerable, return a new CollectionView
            //    (it uses the ListEnumerable wrapper)
            // 8) return null
            // An IListSource must share the view with its underlying list.

            // if the view already exists, just return it
            // Also, return null if it doesn't exist and we're called in "lazy" mode
            ViewRecord viewRecord = GetExistingView(collection, cvs, collectionViewType, GetSourceItem);

            if (viewRecord != null || !createView)
            {
                return(viewRecord);
            }

            // If the collection is an IListSource, it uses the same view as its
            // underlying list.
            IListSource ils     = collection as IListSource;
            IList       ilsList = null;

            if (ils != null)
            {
                ilsList    = ils.GetList();
                viewRecord = GetExistingView(ilsList, cvs, collectionViewType, GetSourceItem);

                if (viewRecord != null)
                {
                    return(CacheView(collection, cvs, (CollectionView)viewRecord.View, viewRecord));
                }
            }

            // Create a new view
            ICollectionView icv = collection as ICollectionView;

            if (icv != null)
            {
                icv = new CollectionViewProxy(icv);
            }
            else if (collectionViewType == null)
            {
                // Caller didn't specify a type for the view.
                ICollectionViewFactory icvf = collection as ICollectionViewFactory;
                if (icvf != null)
                {
                    // collection is a view factory - call its factory method
                    icv = icvf.CreateView();
                }
                else
                {
                    // collection is not a factory - create an appropriate view
                    IList il = (ilsList != null) ? ilsList : collection as IList;
                    if (il != null)
                    {
                        // create a view on an IList or IBindingList
                        IBindingList ibl = il as IBindingList;
                        if (ibl != null)
                        {
                            icv = new BindingListCollectionView(ibl);
                        }
                        else
                        {
                            icv = new ListCollectionView(il);
                        }
                    }
                    else
                    {
                        // collection is not IList, wrap it
                        IEnumerable ie = collection as IEnumerable;
                        if (ie != null)
                        {
                            icv = new EnumerableCollectionView(ie);
                        }
                    }
                }
            }
            else
            {
                // caller specified a type for the view.  Try to honor it.
                if (!typeof(ICollectionView).IsAssignableFrom(collectionViewType))
                {
                    throw new ArgumentException(SR.Get(SRID.CollectionView_WrongType, collectionViewType.Name));
                }

                // if collection is IListSource, get its list first (
                object arg = (ilsList != null) ? ilsList : collection;

                try
                {
                    icv = Activator.CreateInstance(collectionViewType,
                                                   System.Reflection.BindingFlags.CreateInstance, null,
                                                   new object[1] {
                        arg
                    }, null) as ICollectionView;
                }
                catch (MissingMethodException e)
                {
                    throw new ArgumentException(SR.Get(SRID.CollectionView_ViewTypeInsufficient,
                                                       collectionViewType.Name, collection.GetType()), e);
                }
            }

            // if we got a view, add it to the tables
            if (icv != null)
            {
                // if the view doesn't derive from CollectionView, create a proxy that does
                CollectionView cv = icv as CollectionView;
                if (cv == null)
                {
                    cv = new CollectionViewProxy(icv);
                }

                if (ilsList != null)    // IListSource's list shares the same view
                {
                    viewRecord = CacheView(ilsList, cvs, cv, null);
                }

                viewRecord = CacheView(collection, cvs, cv, viewRecord);

                // raise the event for a new view
                BindingOperations.OnCollectionViewRegistering(cv);
            }

            return(viewRecord);
        }
        // Token: 0x060076F8 RID: 30456 RVA: 0x0021FCC8 File Offset: 0x0021DEC8
        internal ViewRecord GetViewRecord(object collection, CollectionViewSource cvs, Type collectionViewType, bool createView, Func <object, object> GetSourceItem)
        {
            ViewRecord viewRecord = this.GetExistingView(collection, cvs, collectionViewType, GetSourceItem);

            if (viewRecord != null || !createView)
            {
                return(viewRecord);
            }
            IListSource listSource = collection as IListSource;
            IList       list       = null;

            if (listSource != null)
            {
                list       = listSource.GetList();
                viewRecord = this.GetExistingView(list, cvs, collectionViewType, GetSourceItem);
                if (viewRecord != null)
                {
                    return(this.CacheView(collection, cvs, (CollectionView)viewRecord.View, viewRecord));
                }
            }
            ICollectionView collectionView = collection as ICollectionView;

            if (collectionView != null)
            {
                collectionView = new CollectionViewProxy(collectionView);
            }
            else if (collectionViewType == null)
            {
                ICollectionViewFactory collectionViewFactory = collection as ICollectionViewFactory;
                if (collectionViewFactory != null)
                {
                    collectionView = collectionViewFactory.CreateView();
                }
                else
                {
                    IList list2 = (list != null) ? list : (collection as IList);
                    if (list2 != null)
                    {
                        IBindingList bindingList = list2 as IBindingList;
                        if (bindingList != null)
                        {
                            collectionView = new BindingListCollectionView(bindingList);
                        }
                        else
                        {
                            collectionView = new ListCollectionView(list2);
                        }
                    }
                    else
                    {
                        IEnumerable enumerable = collection as IEnumerable;
                        if (enumerable != null)
                        {
                            collectionView = new EnumerableCollectionView(enumerable);
                        }
                    }
                }
            }
            else
            {
                if (!typeof(ICollectionView).IsAssignableFrom(collectionViewType))
                {
                    throw new ArgumentException(SR.Get("CollectionView_WrongType", new object[]
                    {
                        collectionViewType.Name
                    }));
                }
                object obj = (list != null) ? list : collection;
                try
                {
                    collectionView = (Activator.CreateInstance(collectionViewType, BindingFlags.CreateInstance, null, new object[]
                    {
                        obj
                    }, null) as ICollectionView);
                }
                catch (MissingMethodException innerException)
                {
                    throw new ArgumentException(SR.Get("CollectionView_ViewTypeInsufficient", new object[]
                    {
                        collectionViewType.Name,
                        collection.GetType()
                    }), innerException);
                }
            }
            if (collectionView != null)
            {
                CollectionView collectionView2 = collectionView as CollectionView;
                if (collectionView2 == null)
                {
                    collectionView2 = new CollectionViewProxy(collectionView);
                }
                if (list != null)
                {
                    viewRecord = this.CacheView(list, cvs, collectionView2, null);
                }
                viewRecord = this.CacheView(collection, cvs, collectionView2, viewRecord);
                BindingOperations.OnCollectionViewRegistering(collectionView2);
            }
            return(viewRecord);
        }