예제 #1
0
        public static Type GetClrType(IEdmType edmType, IEdmModel edmModel, IAssembliesResolver assembliesResolver)
        {
            IEdmSchemaType edmSchemaType = edmType as IEdmSchemaType;

            Contract.Assert(edmSchemaType != null);

            ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmSchemaType);

            if (annotation != null)
            {
                return(annotation.ClrType);
            }

            string             typeName      = edmSchemaType.FullName();
            IEnumerable <Type> matchingTypes = GetMatchingTypes(typeName, assembliesResolver);

            if (matchingTypes.Count() > 1)
            {
                throw Error.Argument("edmTypeReference", SRResources.MultipleMatchingClrTypesForEdmType,
                                     typeName, String.Join(",", matchingTypes.Select(type => type.AssemblyQualifiedName)));
            }

            edmModel.SetAnnotationValue <ClrTypeAnnotation>(edmSchemaType, new ClrTypeAnnotation(matchingTypes.SingleOrDefault()));

            return(matchingTypes.SingleOrDefault());
        }
            private Type GetClrType()
            {
                ClrTypeAnnotation clrTypeAnnotation = _model.GetAnnotationValue <ClrTypeAnnotation>(_type);

                Contract.Assert(clrTypeAnnotation != null);
                Contract.Assert(clrTypeAnnotation.ClrType != null);
                return(clrTypeAnnotation.ClrType);
            }
예제 #3
0
        public static Type GetClrType(IEdmTypeReference edmTypeReference, IEdmModel edmModel)
        {
            if (edmTypeReference == null)
            {
                throw Error.ArgumentNull("edmTypeReference");
            }

            Type primitiveClrType = _builtInTypesMapping
                                    .Where(kvp => edmTypeReference.Definition.IsEquivalentTo(kvp.Value) && (!edmTypeReference.IsNullable || IsNullable(kvp.Key)))
                                    .Select(kvp => kvp.Key)
                                    .FirstOrDefault();

            if (primitiveClrType != null)
            {
                return(primitiveClrType);
            }
            else
            {
                ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmTypeReference.Definition);
                if (annotation != null)
                {
                    return(annotation.ClrType);
                }

                // search all the loaded assemblies for a type with the same name
                IEnumerable <Type> matchingTypes =
                    AppDomain.CurrentDomain
                    .GetAssemblies()
                    .SelectMany(assembly =>
                {
                    Type[] types;
                    try
                    {
                        types = assembly.GetTypes();
                    }
                    catch (ReflectionTypeLoadException e)
                    {
                        types = e.Types;
                    }

                    return(types ?? Enumerable.Empty <Type>());
                })
                    .Where(type => type != null && type.FullName == edmTypeReference.FullName());

                if (matchingTypes.Count() > 1)
                {
                    throw Error.InvalidOperation(SRResources.MultipleMatchingClrTypesForEdmType,
                                                 edmTypeReference.FullName(), String.Join(",", matchingTypes.Select(type => type.AssemblyQualifiedName)));
                }

                edmModel.SetAnnotationValue <ClrTypeAnnotation>(edmTypeReference.Definition, new ClrTypeAnnotation(matchingTypes.SingleOrDefault()));

                return(matchingTypes.SingleOrDefault());
            }
        }
예제 #4
0
        public static IEdmModel AnnotateByClrTypes(this IEdmModel model, IEnumerable <Type> clrTypes)
        {
            var tuples = model.SchemaElements.Join(clrTypes, x => x.Name, x => x.Name, Tuple.Create, StringComparer.OrdinalIgnoreCase);

            foreach (var tuple in tuples)
            {
                var annotation = new ClrTypeAnnotation(tuple.Item2);
                model.SetAnnotationValue(tuple.Item1, annotation);
            }

            return(model);
        }
예제 #5
0
        public static Type GetClrType(this IEdmType edmType, ApiBase api)
        {
            IEdmModel edmModel = api.GetModelAsync().Result;

            ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmType);

            if (annotation != null)
            {
                return(annotation.ClrType);
            }

            throw new NotSupportedException(string.Format(
                                                CultureInfo.InvariantCulture,
                                                Resources.ElementTypeNotFound,
                                                edmType.FullTypeName()));
        }
예제 #6
0
        /// <summary>
        /// Get the clr type for a specified edm type
        /// </summary>
        /// <param name="edmType">The edm type to get clr type</param>
        /// <param name="serviceProvider">The provider to get service from DI container</param>
        /// <returns>The clr type</returns>
        public static Type GetClrType(this IEdmType edmType, IServiceProvider serviceProvider)
        {
            IEdmModel edmModel = serviceProvider.GetService <IEdmModel>();

            ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmType);

            if (annotation != null)
            {
                return(annotation.ClrType);
            }

            throw new NotSupportedException(string.Format(
                                                CultureInfo.InvariantCulture,
                                                Resources.ElementTypeNotFound,
                                                edmType.FullTypeName()));
        }
예제 #7
0
        private static IEdmType GetEdmType(IEdmModel model, Type clrType)
        {
            var edmTypes = model.SchemaElements.OfType <IEdmStructuredType>();

            foreach (var edmType in edmTypes)
            {
                ClrTypeAnnotation annotation = model.GetAnnotationValue <ClrTypeAnnotation>(edmType);
                if (annotation != null)
                {
                    if (annotation.ClrType == clrType)
                    {
                        return(edmType);
                    }
                }
            }

            return(null);
        }
예제 #8
0
        private PropertyInfo GetPropertyInfo(IEdmProperty property)
        {
            ClrPropertyInfoAnnotation clrPropertyAnnotation = _model.GetAnnotationValue <ClrPropertyInfoAnnotation>(property);

            if (clrPropertyAnnotation != null)
            {
                return(clrPropertyAnnotation.ClrPropertyInfo);
            }

            ClrTypeAnnotation clrTypeAnnotation = _model.GetAnnotationValue <ClrTypeAnnotation>(property.DeclaringType);

            Contract.Assert(clrTypeAnnotation != null);

            PropertyInfo info = clrTypeAnnotation.ClrType.GetProperty(property.Name);

            Contract.Assert(info != null);

            return(info);
        }
예제 #9
0
        /// <summary>
        /// Gets the CLR instance type for the specified complex type.
        /// </summary>
        /// <param name="complex">The complex type to get the instance type for.</param>
        /// <param name="canReflectOnInstanceType">true if reflection over the instance type is allowed; otherwise false.</param>
        /// <returns>The CLR instance type to use.</returns>
        protected override Type GetComplexInstanceType(ComplexType complex, out bool canReflectOnInstanceType)
        {
            Type complexType = null;
            ClrTypeAnnotation clrTypeAnnotation = complex.Annotations.OfType <ClrTypeAnnotation>().FirstOrDefault();

            if (clrTypeAnnotation == null)
            {
                complexType = (from exportedType in Workspace.ObjectLayerAssembly.GetExportedTypes()
                               where exportedType.Name == complex.Name
                               select exportedType).SingleOrDefault();
            }
            else
            {
                complexType = clrTypeAnnotation.ClrType;
            }

            ExceptionUtilities.CheckObjectNotNull(complexType, "Could not find complex type '{0}' in object layer", complex.Name);

            canReflectOnInstanceType = true;
            return(complexType);
        }
예제 #10
0
        /// <summary>
        /// Tries to get the relevant type of an entity
        /// set, singleton, or composable function import.
        /// </summary>
        /// <param name="context">
        /// The context for model mapper.
        /// </param>
        /// <param name="name">
        /// The name of an entity set, singleton or composable function import.
        /// </param>
        /// <param name="relevantType">
        /// When this method returns, provides the
        /// relevant type of the queryable source.
        /// </param>
        /// <returns>
        /// <c>true</c> if the relevant type was
        /// provided; otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetRelevantType(
            ModelContext context,
            string name,
            out Type relevantType)
        {
            // Cannot await as cannot make method async
            var model   = context.GetApiService <IEdmModel>();
            var element = model.EntityContainer.Elements.Where(e => e.Name == name).FirstOrDefault();

            if (element != null)
            {
                IEdmType entityType = null;
                var      entitySet  = element as EdmEntitySet;
                if (entitySet != null)
                {
                    var entitySetType = entitySet.Type as EdmCollectionType;
                    entityType = entitySetType.ElementType.Definition;
                }
                else
                {
                    var singleton = element as EdmSingleton;
                    if (singleton != null)
                    {
                        entityType = singleton.Type;
                    }
                }

                if (entityType != null)
                {
                    ClrTypeAnnotation annotation = model.GetAnnotationValue <ClrTypeAnnotation>(entityType);
                    if (annotation != null)
                    {
                        relevantType = annotation.ClrType;
                        return(true);
                    }
                }
            }

            return(InnerMapper.TryGetRelevantType(context, name, out relevantType));
        }
예제 #11
0
        public static Type GetClrType(IEdmTypeReference edmTypeReference, IEdmModel edmModel, IAssembliesResolver assembliesResolver)
        {
            if (edmTypeReference == null)
            {
                throw Error.ArgumentNull("edmTypeReference");
            }

            Type primitiveClrType = _builtInTypesMapping
                                    .Where(kvp => edmTypeReference.Definition.IsEquivalentTo(kvp.Value) && (!edmTypeReference.IsNullable || IsNullable(kvp.Key)))
                                    .Select(kvp => kvp.Key)
                                    .FirstOrDefault();

            if (primitiveClrType != null)
            {
                return(primitiveClrType);
            }
            else
            {
                ClrTypeAnnotation annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmTypeReference.Definition);
                if (annotation != null)
                {
                    return(annotation.ClrType);
                }

                IEnumerable <Type> matchingTypes = GetMatchingTypes(edmTypeReference.FullName(), assembliesResolver);

                if (matchingTypes.Count() > 1)
                {
                    throw Error.Argument("edmTypeReference", SRResources.MultipleMatchingClrTypesForEdmType,
                                         edmTypeReference.FullName(), String.Join(",", matchingTypes.Select(type => type.AssemblyQualifiedName)));
                }

                edmModel.SetAnnotationValue <ClrTypeAnnotation>(edmTypeReference.Definition, new ClrTypeAnnotation(matchingTypes.SingleOrDefault()));

                return(matchingTypes.SingleOrDefault());
            }
        }