Example #1
0
        internal static IBindingList CreateViewForQuery <TElement>(
            TypeUsage elementEdmTypeUsage,
            IEnumerable <TElement> queryResults,
            ObjectContext objectContext,
            bool forceReadOnly,
            EntitySet singleEntitySet)
        {
            Type      type1           = (Type)null;
            TypeUsage ospaceTypeUsage = ObjectViewFactory.GetOSpaceTypeUsage(elementEdmTypeUsage, objectContext);

            if (ospaceTypeUsage == null)
            {
                type1 = typeof(TElement);
            }
            Type         clrType            = ObjectViewFactory.GetClrType <TElement>(ospaceTypeUsage.EdmType);
            object       objectStateManager = (object)objectContext.ObjectStateManager;
            IBindingList bindingList;

            if (clrType == typeof(TElement))
            {
                bindingList = (IBindingList) new ObjectView <TElement>((IObjectViewData <TElement>) new ObjectViewQueryResultData <TElement>((IEnumerable)queryResults, objectContext, forceReadOnly, singleEntitySet), objectStateManager);
            }
            else if (clrType == (Type)null)
            {
                bindingList = (IBindingList) new DataRecordObjectView((IObjectViewData <DbDataRecord>) new ObjectViewQueryResultData <DbDataRecord>((IEnumerable)queryResults, objectContext, true, (EntitySet)null), objectStateManager, (RowType)ospaceTypeUsage.EdmType, typeof(TElement));
            }
            else
            {
                if (!typeof(TElement).IsAssignableFrom(clrType))
                {
                    throw EntityUtil.ValueInvalidCast(clrType, typeof(TElement));
                }
                Type   type2    = ObjectViewFactory._genericObjectViewQueryResultDataType.MakeGenericType(clrType);
                object viewData = type2.GetDeclaredConstructor(typeof(IEnumerable), typeof(ObjectContext), typeof(bool), typeof(EntitySet)).Invoke(new object[4]
                {
                    (object)queryResults,
                    (object)objectContext,
                    (object)forceReadOnly,
                    (object)singleEntitySet
                });
                bindingList = ObjectViewFactory.CreateObjectView(clrType, type2, viewData, objectStateManager);
            }
            return(bindingList);
        }
Example #2
0
        internal static IBindingList CreateViewForEntityCollection <TElement>(
            EntityType entityType,
            EntityCollection <TElement> entityCollection)
            where TElement : class
        {
            TypeUsage ospaceTypeUsage = ObjectViewFactory.GetOSpaceTypeUsage(entityType == null ? (TypeUsage)null : TypeUsage.Create((EdmType)entityType), entityCollection.ObjectContext);
            Type      type1;

            if (ospaceTypeUsage == null)
            {
                type1 = typeof(TElement);
            }
            else
            {
                type1 = ObjectViewFactory.GetClrType <TElement>(ospaceTypeUsage.EdmType);
                if (type1 == (Type)null)
                {
                    type1 = typeof(TElement);
                }
            }
            IBindingList bindingList;

            if (type1 == typeof(TElement))
            {
                bindingList = (IBindingList) new ObjectView <TElement>((IObjectViewData <TElement>) new ObjectViewEntityCollectionData <TElement, TElement>(entityCollection), (object)entityCollection);
            }
            else
            {
                if (!typeof(TElement).IsAssignableFrom(type1))
                {
                    throw EntityUtil.ValueInvalidCast(type1, typeof(TElement));
                }
                Type   type2    = ObjectViewFactory._genericObjectViewEntityCollectionDataType.MakeGenericType(type1, typeof(TElement));
                object viewData = type2.GetDeclaredConstructor(typeof(EntityCollection <TElement>)).Invoke(new object[1]
                {
                    (object)entityCollection
                });
                bindingList = ObjectViewFactory.CreateObjectView(type1, type2, viewData, (object)entityCollection);
            }
            return(bindingList);
        }