Exemplo n.º 1
0
        private TypeUsage TypeUsageForPrimitiveType(Type type)
        {
            bool isNullable = IsNullableType(type);

            type = PrimitiveTypeForType(type);

            //  Find equivalent EdmType in CSpace.  This is a 1-to-1 mapping to CLR types except for the Geometry/Geography types
            //  (so not supporting those atm).
            var primitiveTypeList = _ObjectContext.MetadataWorkspace
                                    .GetPrimitiveTypes(DataSpace.CSpace)
                                    .Where(p => p.ClrEquivalentType == type)
                                    .ToList();

            if (primitiveTypeList.Count != 1)
            {
                throw new ApplicationException(string.Format("Unable to map parameter of type {0} to TypeUsage.  Found {1} matching types", type.Name, primitiveTypeList.Count));
            }
            var primitiveType = primitiveTypeList.FirstOrDefault();

            var facetList = new List <Facet>();

            if (isNullable)
            {
                //  May not even be necessary to specify these Facets, but just to be safe.  And only way to create them is to call the internal Create method...
                var createMethod = typeof(Facet).GetMethod("Create", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(FacetDescription), typeof(object) }, null);

                var facetDescription = Facet.GetGeneralFacetDescriptions().FirstOrDefault(fd => fd.FacetName == "Nullable");
                if (facetDescription != null)
                {
                    facetList.Add((Facet)createMethod.Invoke(null, new object[] { facetDescription, true }));
                }

                facetDescription = Facet.GetGeneralFacetDescriptions().FirstOrDefault(fd => fd.FacetName == "DefaultValue");
                if (facetDescription != null)
                {
                    facetList.Add((Facet)createMethod.Invoke(null, new object[] { facetDescription, null }));
                }
            }

            return(TypeUsage.Create(primitiveType, facetList));
        }