src/Shared/EntityFramework/Core/Objects/ObjectViewFactory.cs
// <summary>
// Return a list suitable for data binding using the supplied EntityCollection
// </summary>
// <typeparam name="TElement"> CLR type of the elements of the EntityCollection. </typeparam>
// <param name="entityType"> The EntityType of the elements in the collection. This should either be the same as the EntityType that corresponds to the CLR TElement type, or a EntityType derived from the declared EntityCollection element type. </param>
// <param name="entityCollection"> The EntityCollection from which a binding list is created. </param>
// <returns>
// <see cref="IBindingList" /> that is suitable for data binding.
// </returns>
Return a list suitable for data binding using the supplied EntityCollection
Return a list suitable for data binding using the supplied EntityCollection
internal static IBindingList CreateViewForEntityCollection<TElement>(
            EntityType entityType, EntityCollection<TElement> entityCollection)
            where TElement : class
        {
            Type clrElementType = null;
            var entityTypeUsage = entityType == null ? null : TypeUsage.Create(entityType);
            var ospaceElementTypeUsage = GetOSpaceTypeUsage(entityTypeUsage, entityCollection.ObjectContext);
            if (ospaceElementTypeUsage == null)
            {
                clrElementType = typeof(TElement);
            }
            else
            {
                clrElementType = GetClrType<TElement>(ospaceElementTypeUsage.EdmType);
                Debug.Assert(clrElementType != null, "clrElementType has unexpected value of null.");
                if (clrElementType == null)
                {
                    clrElementType = typeof(TElement);
                }
            }
            IBindingList objectView;
            if (clrElementType == typeof(TElement))
            {
                var viewData = new ObjectViewEntityCollectionData<TElement, TElement>(entityCollection);
                objectView = new ObjectView<TElement>(viewData, entityCollection);
            }
            else
            {
                if (!typeof(TElement).IsAssignableFrom(clrElementType))
                {
                    throw EntityUtil.ValueInvalidCast(clrElementType, typeof(TElement));
                }
                var objectViewDataType = _genericObjectViewEntityCollectionDataType.MakeGenericType(clrElementType, typeof(TElement));
                var viewDataConstructor = objectViewDataType.GetDeclaredConstructor(typeof(EntityCollection<TElement>));
                Debug.Assert(
                    viewDataConstructor != null,
                    "ObjectViewEntityCollectionData constructor not found. Please ensure constructor signature is correct.");
                var viewData = viewDataConstructor.Invoke(new object[] { entityCollection });
                objectView = CreateObjectView(clrElementType, objectViewDataType, viewData, entityCollection);
            }
            return objectView;
        }
internal static IBindingList CreateViewForEntityCollection < TElement > ( EntityType entityType , EntityCollection < TElement > entityCollection ) where TElement : class { Type clrElementType = null ; var entityTypeUsage = entityType == null ? null : TypeUsage . Create ( entityType ) ; var ospaceElementTypeUsage = GetOSpaceTypeUsage ( entityTypeUsage , entityCollection . ObjectContext ) ; if ( ospaceElementTypeUsage == null ) { clrElementType = typeof ( TElement ) ; } else { clrElementType = GetClrType < TElement > ( ospaceElementTypeUsage . EdmType ) ; Debug . Assert ( clrElementType != null , " " ) ; if ( clrElementType == null ) { clrElementType = typeof ( TElement ) ; } } IBindingList objectView ; if ( clrElementType == typeof ( TElement ) ) { var viewData = new ObjectViewEntityCollectionData < TElement , TElement > ( entityCollection ) ; objectView = new ObjectView < TElement > ( viewData , entityCollection ) ; } else { if ( ! typeof ( TElement ) . IsAssignableFrom ( clrElementType ) ) { throw EntityUtil . ValueInvalidCast ( clrElementType , typeof ( TElement ) ) ; } var objectViewDataType = _genericObjectViewEntityCollectionDataType . MakeGenericType ( clrElementType , typeof ( TElement ) ) ; var viewDataConstructor = objectViewDataType . GetDeclaredConstructor ( typeof ( EntityCollection < TElement > ) ) ; Debug . Assert ( viewDataConstructor != null , " " ) ; var viewData = viewDataConstructor . Invoke ( new object [ ] { entityCollection } ) ; objectView = CreateObjectView ( clrElementType , objectViewDataType , viewData , entityCollection ) ; } return objectView ; }
Return a list suitable for data binding using the supplied EntityCollection
Return a list suitable for data binding using the supplied EntityCollection
// Map the O-Space EDM type to a CLR type. // If the mapping is unsuccessful, fallback to TElement type. // A null clrElementType is returned by GetClrType if the EDM type is a RowType with no specific CLR type mapping. // This should not happen when working with EntityCollections, but if it does, fallback to TEntityRef type. // If the clrElementType matches the declared TElement type, optimize the construction of the ObjectView // by avoiding a reflection-based instantiation. // Use reflection to create an instance of the generic ObjectView and ObjectViewEntityCollectionData classes,  // using clrElementType as the value of TElement generic type parameter for both classes. // Create ObjectViewEntityCollectionData instance // Create ObjectView instance
that is suitable for data binding.
that is suitable for data binding .
entityType
EntityType
The EntityType of the elements in the collection. This should either be the same as the EntityType that corresponds to the CLR TElement type, or a EntityType derived from the declared EntityCollection element type.
The EntityType of the elements in the collection . This should either be the same as the EntityType that corresponds to the CLR TElement type or a EntityType derived from the declared EntityCollection element type .
entityCollection
EntityCollection<TElement>
The EntityCollection from which a binding list is created.
The EntityCollection from which a binding list is created .
typeparam
CLR type of the elements of the EntityCollection.
CLR type of the elements of the EntityCollection .