Ejemplo n.º 1
0
        private void LoadType(Type clrType)
        {
            EdmType edmType = (EdmType)null;
            IEnumerable <EdmTypeAttribute> customAttributes = clrType.GetCustomAttributes <EdmTypeAttribute>(false);

            if (!customAttributes.Any <EdmTypeAttribute>())
            {
                return;
            }
            if (clrType.IsNested)
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.NestedClassNotSupported((object)clrType.FullName, (object)clrType.Assembly().FullName)));
            }
            else
            {
                EdmTypeAttribute edmTypeAttribute = customAttributes.First <EdmTypeAttribute>();
                string           cspaceTypeName   = string.IsNullOrEmpty(edmTypeAttribute.Name) ? clrType.Name : edmTypeAttribute.Name;
                if (string.IsNullOrEmpty(edmTypeAttribute.NamespaceName) && clrType.Namespace == null)
                {
                    this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_TypeHasNoNamespace));
                }
                else
                {
                    string cspaceNamespaceName = string.IsNullOrEmpty(edmTypeAttribute.NamespaceName) ? clrType.Namespace : edmTypeAttribute.NamespaceName;
                    if (edmTypeAttribute.GetType() == typeof(EdmEntityTypeAttribute))
                    {
                        edmType = (EdmType) new ClrEntityType(clrType, cspaceNamespaceName, cspaceTypeName);
                    }
                    else if (edmTypeAttribute.GetType() == typeof(EdmComplexTypeAttribute))
                    {
                        edmType = (EdmType) new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
                    }
                    else
                    {
                        PrimitiveType primitiveType;
                        if (!ClrProviderManifest.Instance.TryGetPrimitiveType(clrType.GetEnumUnderlyingType(), out primitiveType))
                        {
                            this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_UnsupportedEnumUnderlyingType((object)clrType.GetEnumUnderlyingType().FullName)));
                            return;
                        }
                        edmType = (EdmType) new ClrEnumType(clrType, cspaceNamespaceName, cspaceTypeName);
                    }
                    this.CacheEntry.TypesInAssembly.Add(edmType);
                    this.SessionData.TypesInLoading.Add(clrType.FullName, edmType);
                    if (!Helper.IsStructuralType(edmType))
                    {
                        return;
                    }
                    if (Helper.IsEntityType(edmType))
                    {
                        this.TrackClosure(clrType.BaseType());
                        this.AddTypeResolver((Action)(() => edmType.BaseType = this.ResolveBaseType(clrType.BaseType())));
                    }
                    this.LoadPropertiesFromType((StructuralType)edmType);
                }
            }
        }
Ejemplo n.º 2
0
 public static CustomAttributeBuilder CreateCustom(EdmTypeAttribute attribute)
 {
     return(new CustomAttributeBuilder(
                attribute.GetType().GetDeclaredConstructor(),
                new object[0],
                new PropertyInfo[]
     {
         typeof(EdmTypeAttribute).GetDeclaredProperty("Name"),
         typeof(EdmTypeAttribute).GetDeclaredProperty("NamespaceName")
     },
                new object[]
     {
         attribute.Name,
         attribute.NamespaceName
     }));
 }
        /// <summary>
        /// Load metadata of the given type - when you call this method, you should check and make sure that the type has
        /// edm attribute. If it doesn't,we won't load the type and it will be returned as null
        /// </summary>
        /// <param name="clrType"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private void LoadType(Type clrType)
        {
            Debug.Assert(clrType.Assembly == SourceAssembly, "Why are we loading a type that is not in our assembly?");
            Debug.Assert(!SessionData.TypesInLoading.ContainsKey(clrType.FullName), "Trying to load a type that is already loaded???");
            Debug.Assert(!clrType.IsGenericType, "Generic type is not supported");

            EdmType edmType = null;

            EdmTypeAttribute[] typeAttributes = (EdmTypeAttribute[])clrType.GetCustomAttributes(typeof(EdmTypeAttribute), false /*inherit*/);

            // the CLR doesn't allow types to have duplicate/multiple attribute declarations

            if (typeAttributes.Length != 0)
            {
                if (clrType.IsNested)
                {
                    SessionData.EdmItemErrors.Add(new EdmItemError(System.Data.Entity.Strings.NestedClassNotSupported(clrType.FullName, clrType.Assembly.FullName), null));
                    return;
                }
                EdmTypeAttribute typeAttribute  = typeAttributes[0];
                string           cspaceTypeName = String.IsNullOrEmpty(typeAttribute.Name) ? clrType.Name : typeAttribute.Name;
                if (String.IsNullOrEmpty(typeAttribute.NamespaceName) && clrType.Namespace == null)
                {
                    SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_TypeHasNoNamespace, edmType));
                    return;
                }

                string cspaceNamespaceName = String.IsNullOrEmpty(typeAttribute.NamespaceName) ? clrType.Namespace : typeAttribute.NamespaceName;

                if (typeAttribute.GetType() == typeof(EdmEntityTypeAttribute))
                {
                    edmType = new ClrEntityType(clrType, cspaceNamespaceName, cspaceTypeName);
                }
                else if (typeAttribute.GetType() == typeof(EdmComplexTypeAttribute))
                {
                    edmType = new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
                }
                else
                {
                    Debug.Assert(typeAttribute is EdmEnumTypeAttribute, "Invalid type attribute encountered");

                    // Note that TryGetPrimitiveType() will return false not only for types that are not primitive
                    // but also for CLR primitive types that are valid underlying enum types in CLR but are not
                    // a valid Edm primitive types (e.g. ulong)
                    PrimitiveType underlyingEnumType;
                    if (!ClrProviderManifest.Instance.TryGetPrimitiveType(clrType.GetEnumUnderlyingType(), out underlyingEnumType))
                    {
                        SessionData.EdmItemErrors.Add(
                            new EdmItemError(
                                Strings.Validator_UnsupportedEnumUnderlyingType(clrType.GetEnumUnderlyingType().FullName),
                                edmType));

                        return;
                    }

                    edmType = new ClrEnumType(clrType, cspaceNamespaceName, cspaceTypeName);
                }
            }
            else
            {
                // not a type we are interested
                return;
            }

            Debug.Assert(!CacheEntry.ContainsType(edmType.Identity), "This type must not be already present in the list of types for this assembly");
            // Also add this to the list of the types for this assembly
            CacheEntry.TypesInAssembly.Add(edmType);

            // Add this to the known type map so we won't try to load it again
            SessionData.TypesInLoading.Add(clrType.FullName, edmType);

            // Load properties for structural type
            if (Helper.IsStructuralType(edmType))
            {
                //Load base type only for entity type - not sure if we will allow complex type inheritance
                if (Helper.IsEntityType(edmType))
                {
                    TrackClosure(clrType.BaseType);
                    AddTypeResolver(
                        () => edmType.BaseType = ResolveBaseType(clrType.BaseType));
                }

                // Load the properties for this type
                LoadPropertiesFromType((StructuralType)edmType);
            }

            return;
        }