private object GetCollectionInstance(ClientPropertyAnnotation property, out bool instanceCreated) { instanceCreated = false; object obj2 = property.GetValue(this.entity); if (obj2 != null) { return(obj2); } instanceCreated = true; Type propertyType = property.PropertyType; if (BindingEntityInfo.IsDataServiceCollection(propertyType, base.RequestInfo.MaxProtocolVersion)) { object[] args = new object[2]; args[1] = TrackingMode.None; return(Activator.CreateInstance(WebUtil.GetDataServiceCollectionOfT(new Type[] { property.EntityCollectionItemType }), args)); } Type c = typeof(List <>).MakeGenericType(new Type[] { property.EntityCollectionItemType }); if (!propertyType.IsAssignableFrom(c)) { c = propertyType; } return(Activator.CreateInstance(c)); }
/// <summary> /// Determine if the specified type is an DataServiceCollection. /// </summary> /// <remarks> /// If there a generic class in the inheritance hierarchy of the type, that has a single /// entity type paramenter T, and is assignable to DataServiceCollection(Of T), then /// the type is an DataServiceCollection. /// </remarks> /// <param name="collectionType">An object type specifier.</param> /// <param name="model">The client model.</param> /// <returns>true if the type is an DataServiceCollection; otherwise false.</returns> internal static bool IsDataServiceCollection(Type collectionType, ClientEdmModel model) { Debug.Assert(collectionType != null, "Argument 'collectionType' cannot be null."); metadataCacheLock.EnterReadLock(); try { object resultAsObject; if (knownObservableCollectionTypes.TryGetValue(collectionType, out resultAsObject)) { return(resultAsObject == TrueObject); } } finally { metadataCacheLock.ExitReadLock(); } Type type = collectionType; bool result = false; while (type != null) { if (type.IsGenericType()) { // Is there a generic class in the inheritance hierarchy, that has a single // entity type paramenter T, and is assignable to DataServiceCollection<T> Type[] parms = type.GetGenericArguments(); if (parms != null && parms.Length == 1 && IsEntityType(parms[0], model)) { // if ObservableCollection is not available dataServiceCollection will be null Type dataServiceCollection = WebUtil.GetDataServiceCollectionOfT(parms); if (dataServiceCollection != null && dataServiceCollection.IsAssignableFrom(type)) { result = true; break; } } } type = type.GetBaseType(); } metadataCacheLock.EnterWriteLock(); try { if (!knownObservableCollectionTypes.ContainsKey(collectionType)) { knownObservableCollectionTypes[collectionType] = result ? TrueObject : FalseObject; } } finally { metadataCacheLock.ExitWriteLock(); } return(result); }
internal static bool IsDataServiceCollection(Type collectionType) { Debug.Assert(collectionType != null, "Argument 'collectionType' cannot be null."); metadataCacheLock.EnterReadLock(); try { object resultAsObject; if (knownObservableCollectionTypes.TryGetValue(collectionType, out resultAsObject)) { return(resultAsObject == TrueObject); } } finally { metadataCacheLock.ExitReadLock(); } Type type = collectionType; bool result = false; while (type != null) { if (type.IsGenericType) { Type[] parms = type.GetGenericArguments(); if (parms != null && parms.Length == 1 && IsEntityType(parms[0])) { Type dataServiceCollection = WebUtil.GetDataServiceCollectionOfT(parms); if (dataServiceCollection != null && dataServiceCollection.IsAssignableFrom(type)) { result = true; break; } } } type = type.BaseType; } metadataCacheLock.EnterWriteLock(); try { if (!knownObservableCollectionTypes.ContainsKey(collectionType)) { knownObservableCollectionTypes[collectionType] = result ? TrueObject : FalseObject; } } finally { metadataCacheLock.ExitWriteLock(); } return(result); }
internal static bool IsCollectionProducingExpression(Expression e) { if (TypeSystem.FindIEnumerable(e.Type) != null) { Type elementType = TypeSystem.GetElementType(e.Type); Type dataServiceCollectionOfT = WebUtil.GetDataServiceCollectionOfT(new Type[] { elementType }); if (typeof(List <>).MakeGenericType(new Type[] { elementType }).IsAssignableFrom(e.Type) || ((dataServiceCollectionOfT != null) && dataServiceCollectionOfT.IsAssignableFrom(e.Type))) { return(true); } } return(false); }
internal static bool IsDataServiceCollection(Type collectionType, DataServiceProtocolVersion maxProtocolVersion) { metadataCacheLock.EnterReadLock(); try { object obj2; if (knownObservableCollectionTypes.TryGetValue(collectionType, out obj2)) { return(obj2 == TrueObject); } } finally { metadataCacheLock.ExitReadLock(); } Type baseType = collectionType; bool flag = false; while (baseType != null) { if (baseType.IsGenericType()) { Type[] genericArguments = baseType.GetGenericArguments(); if (((genericArguments != null) && (genericArguments.Length == 1)) && IsEntityType(genericArguments[0], maxProtocolVersion)) { Type dataServiceCollectionOfT = WebUtil.GetDataServiceCollectionOfT(genericArguments); if ((dataServiceCollectionOfT != null) && dataServiceCollectionOfT.IsAssignableFrom(baseType)) { flag = true; break; } } } baseType = baseType.BaseType; } metadataCacheLock.EnterWriteLock(); try { if (!knownObservableCollectionTypes.ContainsKey(collectionType)) { knownObservableCollectionTypes[collectionType] = flag ? TrueObject : FalseObject; } } finally { metadataCacheLock.ExitWriteLock(); } return(flag); }
internal static bool IsCollectionProducingExpression(Expression e) { if (TypeSystem.FindIEnumerable(e.Type) != null) { Type elementType = TypeSystem.GetElementType(e.Type); Debug.Assert(elementType != null, "elementType == null"); Type dscType = WebUtil.GetDataServiceCollectionOfT(elementType); if (typeof(List <>).MakeGenericType(elementType).IsAssignableFrom(e.Type) || (dscType != null && dscType.IsAssignableFrom(e.Type))) { return(true); } } return(false); }