Exemple #1
0
        private static Type[] GetTypeHierarchy(Type type, out PropertyInfo[] keyProperties, out bool hasProperties)
        {
            keyProperties = ClientTypeUtil.GetKeyPropertiesOnType(type, out hasProperties);
            List <Type> list = new List <Type>();

            if (keyProperties != null)
            {
                Type declaringType;
                if (keyProperties.Length > 0)
                {
                    declaringType = keyProperties[0].DeclaringType;
                }
                else
                {
                    declaringType = type;
                    while (!declaringType.GetCustomAttributes(false).OfType <DataServiceEntityAttribute>().Any <DataServiceEntityAttribute>() && (declaringType.GetBaseType() != null))
                    {
                        declaringType = declaringType.GetBaseType();
                    }
                }
                do
                {
                    list.Insert(0, type);
                }while ((type != declaringType) && ((type = type.GetBaseType()) != null));
            }
            else
            {
                do
                {
                    list.Insert(0, type);
                }while (((type = type.GetBaseType()) != null) && ClientTypeUtil.GetPropertiesOnType(type, false).Any <PropertyInfo>());
            }
            return(list.ToArray());
        }
Exemple #2
0
        /// <summary>Returns <paramref name="type"/> and its base types, in the order of most base type first and <paramref name="type"/> last.</summary>
        /// <param name="type">Type instance in question.</param>
        /// <param name="keyProperties">Returns the list of key properties if <paramref name="type"/> is an entity type; null otherwise.</param>
        /// <param name="hasProperties">true if <paramref name="type"/> has any (declared or inherited) properties; otherwise false.</param>
        /// <returns>Returns <paramref name="type"/> and its base types, in the order of most base type first and <paramref name="type"/> last.</returns>
        private static Type[] GetTypeHierarchy(Type type, out PropertyInfo[] keyProperties, out bool hasProperties)
        {
            Debug.Assert(type != null, "type != null");

            keyProperties = ClientTypeUtil.GetKeyPropertiesOnType(type, out hasProperties);

            List <Type> hierarchy = new List <Type>();

            if (keyProperties != null)
            {
                // type is an entity. Return all types between keyPropertyDeclaredType and type inclusive.
                Type baseEntityType;
                if (keyProperties.Length > 0)
                {
                    baseEntityType = keyProperties[0].DeclaringType;
                }
                else
                {
                    // Find the type where the DataServiceEntityAttribute is declared on.
                    baseEntityType = type;
                    Debug.Assert(type.GetCustomAttributes(true).OfType <EntityTypeAttribute>().Any(), "type.GetCustomAttributes(true).OfType<DataServiceEntityAttribute>().Any()");
                    while (!baseEntityType.GetCustomAttributes(false).OfType <EntityTypeAttribute>().Any() && c.PlatformHelper.GetBaseType(baseEntityType) != null)
                    {
                        baseEntityType = c.PlatformHelper.GetBaseType(baseEntityType);
                    }

                    Debug.Assert(baseEntityType != null, "keyPropertyDeclaringType != null");
                }

                do
                {
                    hierarchy.Insert(0, type);
                }while (type != baseEntityType && (type = c.PlatformHelper.GetBaseType(type)) != null);
            }
            else
            {
                // type is a complex type. Return all types on the hierarchy where there are properties defined.
                do
                {
                    hierarchy.Insert(0, type);
                }while ((type = c.PlatformHelper.GetBaseType(type)) != null && ClientTypeUtil.GetPropertiesOnType(type, false /*declaredOnly*/).Any());
            }

            return(hierarchy.ToArray());
        }
 /// <summary>
 /// Is the type or element type (in the case of nullableOfT or IEnumOfT) a Entity Type?
 /// </summary>
 /// <param name="type">Type to examine</param>
 /// <returns>bool indicating whether or not entity type</returns>
 internal static bool TypeOrElementTypeIsEntity(Type type)
 {
     type = TypeSystem.GetElementType(type);
     type = Nullable.GetUnderlyingType(type) ?? type;
     return(!PrimitiveType.IsKnownType(type) && ClientTypeUtil.GetKeyPropertiesOnType(type) != null);
 }