EdmTypeReference

We found 10 examples in language CSharp for this search. You will see 33 fragments of code.
        public static bool IsEqual(this IEdmTypeReference @this, IEdmTypeReference edmTypeReference)
        {
            if (@this == edmTypeReference)
                return true;
            if (@this == null || edmTypeReference == null)
                return false;

            return @this.Definition == edmTypeReference.Definition && @this.IsNullable == edmTypeReference.IsNullable;
        }

        public static bool IsEqual(this IEdmTypeReference @this, IEdmTypeReference edmTypeReference)
        {
            if (@this == edmTypeReference)
                return true;
            if (@this == null || edmTypeReference == null)
                return false;

            return @this.Definition == edmTypeReference.Definition && @this.IsNullable == edmTypeReference.IsNullable;
        }


        #endregion

        #region Private Methods

        private static bool IsEdmTypeAlreadyRegistered(IEdmTypeReference edmTypeReference)
        {
            return customUriLiteralParserPerEdmType.Any(uriParserOfEdmType =>
                EdmElementComparer.IsEquivalentTo(uriParserOfEdmType.EdmTypeOfUriParser, edmTypeReference));
        }

        public static bool IsEqual(this IEdmTypeReference @this, IEdmTypeReference edmTypeReference)
        {
            if (@this == edmTypeReference)
                return true;
            if (@this == null || edmTypeReference == null)
                return false;

            return @this.Definition == edmTypeReference.Definition && @this.IsNullable == edmTypeReference.IsNullable;
        }


        /// <summary>
        /// Convert an OData value into a CLR object.
        /// </summary>
        /// <param name="graph">The given object.</param>
        /// <param name="edmTypeReference">The EDM type of the given object.</param>
        /// <param name="clrType">The CLR type of the given object.</param>
        /// <param name="parameterName">The parameter name of the given object.</param>
        /// <param name="readContext">The <see cref="ODataDeserializerContext"/> use to convert.</param>
        /// <param name="requestContainer">The dependency injection container for the request.</param>
        /// <returns>The converted object.</returns>
        public static object Convert(object graph, IEdmTypeReference edmTypeReference,
            Type clrType, string parameterName, ODataDeserializerContext readContext,
            IServiceProvider requestContainer)
        {
            if (graph == null || graph is ODataNullValue)
            {
                return null;
            }

            ODataDeserializerProvider deserializerProvider =
                requestContainer.GetRequiredService<ODataDeserializerProvider>();
            // collection of primitive, enum
            ODataCollectionValue collectionValue = graph as ODataCollectionValue;
            if (collectionValue != null)
            {
                return ConvertCollection(collectionValue, edmTypeReference, clrType, parameterName, readContext,
                    requestContainer);
            }

            // enum value
            ODataEnumValue enumValue = graph as ODataEnumValue;
            if (enumValue != null)
            {
                IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum();
                Contract.Assert(edmEnumType != null);

                ODataEnumDeserializer deserializer =
                    (ODataEnumDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                return deserializer.ReadInline(enumValue, edmEnumType, readContext);
            }

            // primitive value
            if (edmTypeReference.IsPrimitive())
            {
                return EdmPrimitiveHelpers.ConvertPrimitiveValue(graph, clrType);
            }

            // Resource, ResourceSet, Entity Reference or collection of entity reference
            return ConvertResourceOrResourceSet(graph, edmTypeReference, readContext);
        }

        internal static object ConvertTo(string valueString, Type type)
        {
            if (valueString == null)
            {
                return null;
            }

            if (TypeHelper.IsNullable(type) && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return null;
            }

            // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal
            // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue
            // to uri literal.
            if (TypeHelper.IsEnum(type))
            {
                string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None);
                if (values.Length == 3 && String.IsNullOrEmpty(values[2]))
                {
                    // Remove the type name if the enum value is a fully qualified literal.
                    valueString = values[1];
                }

                Type enumType = TypeHelper.GetUnderlyingTypeOrSelf(type);
                object[] parameters = new[] { valueString, Enum.ToObject(enumType, 0) };
                bool isSuccessful = (bool)EnumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters);

                if (!isSuccessful)
                {
                    throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name);
                }

                return parameters[1];
            }

            // The logic of "public static object ConvertFromUriLiteral(string value, ODataVersion version);" treats
            // the date value string (for example: 2015-01-02) as DateTimeOffset literal, and return a DateTimeOffset
            // object. However, the logic of
            // "object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference);"
            // can return the correct Date object.
            if (type == typeof(Date) || type == typeof(Date?))
            {
                EdmCoreModel model = EdmCoreModel.Instance;
                IEdmPrimitiveTypeReference dateTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(type);
                return ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4, model, dateTypeReference);
            }

            object value;
            try
            {
                value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);
            }
            catch
            {
                if (type == typeof(string))
                {
                    return valueString;
                }

                throw;
            }

            bool isNonStandardEdmPrimitive;
            EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);

            if (isNonStandardEdmPrimitive)
            {
                return EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type);
            }
            else
            {
                type = Nullable.GetUnderlyingType(type) ?? type;
                return System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
            }
        }

        private static object ConvertCollection(ODataCollectionValue collectionValue,
            IEdmTypeReference edmTypeReference, Type clrType, string parameterName,
            ODataDeserializerContext readContext, IServiceProvider requestContainer)
        {
            Contract.Assert(collectionValue != null);

            IEdmCollectionTypeReference collectionType = edmTypeReference as IEdmCollectionTypeReference;
            Contract.Assert(collectionType != null);

            ODataDeserializerProvider deserializerProvider =
                requestContainer.GetRequiredService<ODataDeserializerProvider>();
            ODataCollectionDeserializer deserializer =
                (ODataCollectionDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType);

            object value = deserializer.ReadInline(collectionValue, collectionType, readContext);
            if (value == null)
            {
                return null;
            }

            IEnumerable collection = value as IEnumerable;
            Contract.Assert(collection != null);

            Type elementType;
            if (!TypeHelper.IsCollection(clrType, out elementType))
            {
                // EdmEntityCollectionObject and EdmComplexCollectionObject are collection types.
                throw new ODataException(String.Format(CultureInfo.InvariantCulture,
                    SRResources.ParameterTypeIsNotCollection, parameterName, clrType));
            }

            IEnumerable newCollection;
            if (CollectionDeserializationHelpers.TryCreateInstance(clrType, collectionType, elementType,
                out newCollection))
            {
                collection.AddToCollection(newCollection, elementType, parameterName, clrType);
                if (clrType.IsArray)
                {
                    newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType);
                }

                return newCollection;
            }

            return null;
        }

        private static object ConvertResourceOrResourceSet(object oDataValue, IEdmTypeReference edmTypeReference,
            ODataDeserializerContext readContext)
        {
            string valueString = oDataValue as string;
            Contract.Assert(valueString != null);

            if (edmTypeReference.IsNullable && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return null;
            }

            IWebApiRequestMessage request = readContext.InternalRequest;
            ODataMessageReaderSettings oDataReaderSettings = request.ReaderSettings;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(valueString)))
            {
                stream.Seek(0, SeekOrigin.Begin);

                IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(stream, null,
                    request.ODataContentIdMapping);
                using (
                    ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage,
                        oDataReaderSettings, readContext.Model))
                {
                    if (edmTypeReference.IsCollection())
                    {
                        return ConvertResourceSet(oDataMessageReader, edmTypeReference, readContext);
                    }
                    else
                    {
                        return ConvertResource(oDataMessageReader, edmTypeReference, readContext);
                    }
                }
            }
        }

        private static object ConvertResourceSet(ODataMessageReader oDataMessageReader,
            IEdmTypeReference edmTypeReference, ODataDeserializerContext readContext)
        {
            IEdmCollectionTypeReference collectionType = edmTypeReference.AsCollection();

            EdmEntitySet tempEntitySet = null;
            if (collectionType.ElementType().IsEntity())
            {
                tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp",
                    collectionType.ElementType().AsEntity().EntityDefinition());
            }

            // TODO: Sam xu, can we use the parameter-less overload
            ODataReader odataReader = oDataMessageReader.CreateODataUriParameterResourceSetReader(tempEntitySet,
                collectionType.ElementType().AsStructured().StructuredDefinition());
            ODataResourceSetWrapper resourceSet =
                odataReader.ReadResourceOrResourceSet() as ODataResourceSetWrapper;

            ODataDeserializerProvider deserializerProvider = readContext.InternalRequest.DeserializerProvider;

            ODataResourceSetDeserializer resourceSetDeserializer =
                (ODataResourceSetDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType);

            object result = resourceSetDeserializer.ReadInline(resourceSet, collectionType, readContext);
            IEnumerable enumerable = result as IEnumerable;
            if (enumerable != null)
            {
                IEnumerable newEnumerable = enumerable;
                if (collectionType.ElementType().IsEntity())
                {
                    newEnumerable = CovertResourceSetIds(enumerable, resourceSet, collectionType, readContext);
                }

                if (readContext.IsUntyped)
                {
                    return newEnumerable.ConvertToEdmObject(collectionType);
                }
                else
                {
                    IEdmTypeReference elementTypeReference = collectionType.ElementType();

                    Type elementClrType = EdmLibHelpers.GetClrType(elementTypeReference,
                        readContext.Model);
                    IEnumerable castedResult =
                        CastMethodInfo.MakeGenericMethod(elementClrType)
                            .Invoke(null, new object[] { newEnumerable }) as IEnumerable;
                    return castedResult;
                }
            }

            return null;
        }

        private static object ConvertResource(ODataMessageReader oDataMessageReader, IEdmTypeReference edmTypeReference,
            ODataDeserializerContext readContext)
        {
            EdmEntitySet tempEntitySet = null;
            if (edmTypeReference.IsEntity())
            {
                IEdmEntityTypeReference entityType = edmTypeReference.AsEntity();
                tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp",
                    entityType.EntityDefinition());
            }

            // TODO: Sam xu, can we use the parameter-less overload
            ODataReader resourceReader = oDataMessageReader.CreateODataUriParameterResourceReader(tempEntitySet,
                edmTypeReference.ToStructuredType());

            object item = resourceReader.ReadResourceOrResourceSet();

            ODataResourceWrapper topLevelResource = item as ODataResourceWrapper;
            Contract.Assert(topLevelResource != null);

            ODataDeserializerProvider deserializerProvider = readContext.InternalRequest.DeserializerProvider;

            ODataResourceDeserializer entityDeserializer =
                (ODataResourceDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmTypeReference);
            object value = entityDeserializer.ReadInline(topLevelResource, edmTypeReference, readContext);

            if (edmTypeReference.IsEntity())
            {
                IEdmEntityTypeReference entityType = edmTypeReference.AsEntity();
                return CovertResourceId(value, topLevelResource.Resource, entityType, readContext);
            }

            return value;
        }


        /// <summary>
        /// Convert an OData value into a CLR object.
        /// </summary>
        /// <param name="graph">The given object.</param>
        /// <param name="edmTypeReference">The EDM type of the given object.</param>
        /// <param name="clrType">The CLR type of the given object.</param>
        /// <param name="parameterName">The parameter name of the given object.</param>
        /// <param name="readContext">The <see cref="ODataDeserializerContext"/> use to convert.</param>
        /// <param name="requestContainer">The dependency injection container for the request.</param>
        /// <returns>The converted object.</returns>
        public static object Convert(object graph, IEdmTypeReference edmTypeReference,
            Type clrType, string parameterName, ODataDeserializerContext readContext,
            IServiceProvider requestContainer)
        {
            if (graph == null || graph is ODataNullValue)
            {
                return null;
            }

            // collection of primitive, enum
            ODataCollectionValue collectionValue = graph as ODataCollectionValue;
            if (collectionValue != null)
            {
                return ConvertCollection(collectionValue, edmTypeReference, clrType, parameterName, readContext,
                    requestContainer);
            }

            // enum value
            ODataEnumValue enumValue = graph as ODataEnumValue;
            if (enumValue != null)
            {
                IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum();
                Contract.Assert(edmEnumType != null);

                ODataDeserializerProvider deserializerProvider =
                    requestContainer.GetRequiredService<ODataDeserializerProvider>();

                ODataEnumDeserializer deserializer =
                    (ODataEnumDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                return deserializer.ReadInline(enumValue, edmEnumType, readContext);
            }

            // primitive value
            if (edmTypeReference.IsPrimitive())
            {
                ConstantNode node = graph as ConstantNode;
                return EdmPrimitiveHelpers.ConvertPrimitiveValue(node != null ? node.Value : graph, clrType);
            }

            // Resource, ResourceSet, Entity Reference or collection of entity reference
            return ConvertResourceOrResourceSet(graph, edmTypeReference, readContext);
        }

        internal static object ConvertTo(string valueString, Type type)
        {
            if (valueString == null)
            {
                return null;
            }

            if (TypeHelper.IsNullable(type) && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return null;
            }

            // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal
            // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue
            // to uri literal.
            if (TypeHelper.IsEnum(type))
            {
                string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None);
                if (values.Length == 3 && String.IsNullOrEmpty(values[2]))
                {
                    // Remove the type name if the enum value is a fully qualified literal.
                    valueString = values[1];
                }

                Type enumType = TypeHelper.GetUnderlyingTypeOrSelf(type);
                object[] parameters = new[] { valueString, Enum.ToObject(enumType, 0) };
                bool isSuccessful = (bool)EnumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters);

                if (!isSuccessful)
                {
                    throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name);
                }

                return parameters[1];
            }

            // The logic of "public static object ConvertFromUriLiteral(string value, ODataVersion version);" treats
            // the date value string (for example: 2015-01-02) as DateTimeOffset literal, and return a DateTimeOffset
            // object. However, the logic of
            // "object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference);"
            // can return the correct Date object.
            if (type == typeof(Date) || type == typeof(Date?))
            {
                EdmCoreModel model = EdmCoreModel.Instance;
                IEdmPrimitiveTypeReference dateTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(type);
                return ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4, model, dateTypeReference);
            }

            object value;
            try
            {
                value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);
            }
            catch
            {
                if (type == typeof(string))
                {
                    return valueString;
                }

                throw;
            }

            bool isNonStandardEdmPrimitive;
            EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);

            if (isNonStandardEdmPrimitive)
            {
                return EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type);
            }
            else
            {
                type = Nullable.GetUnderlyingType(type) ?? type;
                return System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
            }
        }

        private static object ConvertCollection(ODataCollectionValue collectionValue,
            IEdmTypeReference edmTypeReference, Type clrType, string parameterName,
            ODataDeserializerContext readContext, IServiceProvider requestContainer)
        {
            Contract.Assert(collectionValue != null);

            IEdmCollectionTypeReference collectionType = edmTypeReference as IEdmCollectionTypeReference;
            Contract.Assert(collectionType != null);

            ODataDeserializerProvider deserializerProvider =
                requestContainer.GetRequiredService<ODataDeserializerProvider>();
            ODataCollectionDeserializer deserializer =
                (ODataCollectionDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType);

            object value = deserializer.ReadInline(collectionValue, collectionType, readContext);
            if (value == null)
            {
                return null;
            }

            IEnumerable collection = value as IEnumerable;
            Contract.Assert(collection != null);

            Type elementType;
            if (!TypeHelper.IsCollection(clrType, out elementType))
            {
                // EdmEntityCollectionObject and EdmComplexCollectionObject are collection types.
                throw new ODataException(String.Format(CultureInfo.InvariantCulture,
                    SRResources.ParameterTypeIsNotCollection, parameterName, clrType));
            }

            IEnumerable newCollection;
            if (CollectionDeserializationHelpers.TryCreateInstance(clrType, collectionType, elementType,
                out newCollection))
            {
                collection.AddToCollection(newCollection, elementType, parameterName, clrType);
                if (clrType.IsArray)
                {
                    newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType);
                }

                return newCollection;
            }

            return null;
        }

        private static object ConvertResourceOrResourceSet(object oDataValue, IEdmTypeReference edmTypeReference,
            ODataDeserializerContext readContext)
        {
            string valueString = oDataValue as string;
            Contract.Assert(valueString != null);

            if (edmTypeReference.IsNullable && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return null;
            }

            IWebApiRequestMessage request = readContext.InternalRequest;
            ODataMessageReaderSettings oDataReaderSettings = request.ReaderSettings;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(valueString)))
            {
                stream.Seek(0, SeekOrigin.Begin);

                IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(stream, null,
                    request.ODataContentIdMapping);
                using (
                    ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage,
                        oDataReaderSettings, readContext.Model))
                {
                    if (edmTypeReference.IsCollection())
                    {
                        return ConvertResourceSet(oDataMessageReader, edmTypeReference, readContext);
                    }
                    else
                    {
                        return ConvertResource(oDataMessageReader, edmTypeReference, readContext);
                    }
                }
            }
        }

        private static object ConvertResourceSet(ODataMessageReader oDataMessageReader,
            IEdmTypeReference edmTypeReference, ODataDeserializerContext readContext)
        {
            IEdmCollectionTypeReference collectionType = edmTypeReference.AsCollection();

            EdmEntitySet tempEntitySet = null;
            if (collectionType.ElementType().IsEntity())
            {
                tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp",
                    collectionType.ElementType().AsEntity().EntityDefinition());
            }

            // TODO: Sam xu, can we use the parameter-less overload
            ODataReader odataReader = oDataMessageReader.CreateODataUriParameterResourceSetReader(tempEntitySet,
                collectionType.ElementType().AsStructured().StructuredDefinition());
            ODataResourceSetWrapper resourceSet =
                odataReader.ReadResourceOrResourceSet() as ODataResourceSetWrapper;

            ODataDeserializerProvider deserializerProvider = readContext.InternalRequest.DeserializerProvider;

            ODataResourceSetDeserializer resourceSetDeserializer =
                (ODataResourceSetDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType);

            object result = resourceSetDeserializer.ReadInline(resourceSet, collectionType, readContext);
            IEnumerable enumerable = result as IEnumerable;
            if (enumerable != null)
            {
                IEnumerable newEnumerable = enumerable;
                if (collectionType.ElementType().IsEntity())
                {
                    newEnumerable = CovertResourceSetIds(enumerable, resourceSet, collectionType, readContext);
                }

                if (readContext.IsUntyped)
                {
                    return newEnumerable.ConvertToEdmObject(collectionType);
                }
                else
                {
                    IEdmTypeReference elementTypeReference = collectionType.ElementType();

                    Type elementClrType = EdmLibHelpers.GetClrType(elementTypeReference,
                        readContext.Model);
                    IEnumerable castedResult =
                        CastMethodInfo.MakeGenericMethod(elementClrType)
                            .Invoke(null, new object[] { newEnumerable }) as IEnumerable;
                    return castedResult;
                }
            }

            return null;
        }

        private static object ConvertResource(ODataMessageReader oDataMessageReader, IEdmTypeReference edmTypeReference,
            ODataDeserializerContext readContext)
        {
            EdmEntitySet tempEntitySet = null;
            if (edmTypeReference.IsEntity())
            {
                IEdmEntityTypeReference entityType = edmTypeReference.AsEntity();
                tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp",
                    entityType.EntityDefinition());
            }

            // TODO: Sam xu, can we use the parameter-less overload
            ODataReader resourceReader = oDataMessageReader.CreateODataUriParameterResourceReader(tempEntitySet,
                edmTypeReference.ToStructuredType());

            object item = resourceReader.ReadResourceOrResourceSet();

            ODataResourceWrapper topLevelResource = item as ODataResourceWrapper;
            Contract.Assert(topLevelResource != null);

            ODataDeserializerProvider deserializerProvider = readContext.InternalRequest.DeserializerProvider;

            ODataResourceDeserializer entityDeserializer =
                (ODataResourceDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmTypeReference);
            object value = entityDeserializer.ReadInline(topLevelResource, edmTypeReference, readContext);

            if (edmTypeReference.IsEntity())
            {
                IEdmEntityTypeReference entityType = edmTypeReference.AsEntity();
                return CovertResourceId(value, topLevelResource.Resource, entityType, readContext);
            }

            return value;
        }


        public static IEdmType GetEdmType(this IEdmModel edmModel, Type clrType)
        {
            if (edmModel == null)
            {
                throw Error.ArgumentNull("edmModel");
            }

            IEdmPrimitiveType primitiveType;
            if (_builtInTypesMapping.TryGetValue(clrType, out primitiveType))
            {
                return primitiveType;
            }
            else
            {
                Type enumerableOfT = ExtractGenericInterface(clrType, typeof(IEnumerable<>));
                if (enumerableOfT != null)
                {
                    IEdmTypeReference elementType = GetEdmTypeReference(edmModel, enumerableOfT.GetGenericArguments()[0]);
                    if (elementType != null)
                    {
                        return new EdmCollectionType(elementType);
                    }
                }

                // search for the ClrTypeAnnotation and return it if present
                IEdmType returnType =
                    edmModel
                    .SchemaElements
                    .OfType<IEdmType>()
                    .Select(edmType => new { EdmType = edmType, Annotation = edmModel.GetAnnotationValue<ClrTypeAnnotation>(edmType) })
                    .Where(tuple => tuple.Annotation != null && tuple.Annotation.ClrType == clrType)
                    .Select(tuple => tuple.EdmType)
                    .SingleOrDefault();

                // default to the EdmType with the same name as the ClrType name 
                returnType = returnType ?? edmModel.FindType(clrType.FullName);
                return returnType;
            }
        }

        public static IEdmTypeReference GetEdmTypeReference(this IEdmModel edmModel, Type clrType)
        {
            IEdmType edmType = edmModel.GetEdmType(clrType);
            if (edmType != null)
            {
                bool isNullable = IsNullable(clrType);
                switch (edmType.TypeKind)
                {
                    case EdmTypeKind.Collection:
                        return new EdmCollectionTypeReference(edmType as IEdmCollectionType, isNullable);
                    case EdmTypeKind.Complex:
                        return new EdmComplexTypeReference(edmType as IEdmComplexType, isNullable);
                    case EdmTypeKind.Entity:
                        return new EdmEntityTypeReference(edmType as IEdmEntityType, isNullable);
                    case EdmTypeKind.EntityReference:
                        return new EdmEntityReferenceTypeReference(edmType as IEdmEntityReferenceType, isNullable);
                    case EdmTypeKind.Enum:
                        return new EdmEnumTypeReference(edmType as IEdmEnumType, isNullable);
                    case EdmTypeKind.Primitive:
                        return _coreModel.GetPrimitive((edmType as IEdmPrimitiveType).PrimitiveKind, isNullable);
                    case EdmTypeKind.Row:
                        return new EdmRowTypeReference(edmType as IEdmRowType, isNullable);
                    default:
                        throw Error.NotSupported(SRResources.EdmTypeNotSupported, edmType.ToTraceString());
                }
            }

            return null;
        }

        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();
            }
        }

        public static IEdmPrimitiveType GetEdmPrimitiveTypeOrNull(Type clrType)
        {
            IEdmPrimitiveType primitiveType;
            return _builtInTypesMapping.TryGetValue(clrType, out primitiveType) ? primitiveType : null;
        }

        public static IEdmPrimitiveTypeReference GetEdmPrimitiveTypeReferenceOrNull(Type clrType)
        {
            IEdmPrimitiveType primitiveType = GetEdmPrimitiveTypeOrNull(clrType);
            return primitiveType != null ? new EdmPrimitiveTypeReference(primitiveType, IsNullable(clrType)) : null;
        }

        private static IEdmPrimitiveType GetPrimitiveType(EdmPrimitiveTypeKind primitiveKind)
        {
            return _coreModel.GetPrimitiveType(primitiveKind);
        }

        public static bool IsEqual(this IEdmTypeReference @this, IEdmTypeReference edmTypeReference)
        {
            if (@this == edmTypeReference)
                return true;
            if (@this == null || edmTypeReference == null)
                return false;

            return @this.Definition == edmTypeReference.Definition && @this.IsNullable == edmTypeReference.IsNullable;
        }


        public void AddComplexType(Type complexType)
        {
            var edmComplexType = new EdmComplexType(complexType.Namespace, complexType.Name);
            _complexTypes.Add(complexType, edmComplexType);
        }
        public void AddEnumType(Type enumType)
        {
            _enumTypes.Add(enumType, CreateEdmEnumType(enumType));
        }
        public void AddOperation(OeOperationConfiguration operationConfiguration)
        {
            _operationConfigurations.Add(operationConfiguration);
        }
        private void AddOperations()
        {
            IReadOnlyList<OeOperationConfiguration> operations = _dataAdapter.OperationAdapter.GetOperations();
            if (operations != null)
                for (int i = 0; i < operations.Count; i++)
                    AddOperation(operations[i]);
        }
        private EdmAction BuildAction(OeOperationConfiguration operationConfiguration, Dictionary<Type, EntityTypeInfo> entityTypeInfos)
        {
            var edmAction = new EdmAction(operationConfiguration.NamespaceName, operationConfiguration.Name, null);
            foreach (OeOperationParameterConfiguration parameterConfiguration in operationConfiguration.Parameters)
            {
                IEdmTypeReference edmTypeReference = GetEdmTypeReference(parameterConfiguration.ClrType, entityTypeInfos);
                edmAction.AddParameter(parameterConfiguration.Name, edmTypeReference);
            }

            return edmAction;
        }
        public EdmModel BuildEdmModel(Type[] excludedTypes, params IEdmModel[] refModels)
        {
            AddOperations();
            var edmModel = new EdmModel(false);

            Dictionary<Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes(refModels, excludedTypes);
            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
                if (!typeInfo.IsRefModel)
                    typeInfo.BuildStructuralProperties(edmModel, entityTypeInfos, _enumTypes, _complexTypes);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
                if (!typeInfo.IsRefModel)
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                        fkeyInfo.BuildNavigationProperty();

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair<Type, EdmEnumType> enumType in _enumTypes)
                edmModel.SetClrType(enumType.Value, enumType.Key);

            edmModel.AddElements(_complexTypes.Values);
            foreach (KeyValuePair<Type, EdmComplexType> complexType in _complexTypes)
                edmModel.SetClrType(complexType.Value, complexType.Key);

            var container = new EdmEntityContainer(_dataAdapter.DataContextType.Namespace, _dataAdapter.DataContextType.Name);
            edmModel.SetDataAdapter(container, _dataAdapter);

            var entitySets = new Dictionary<IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count);
            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    edmModel.AddElement(typeInfo.EdmType);
                    edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType);
                }

                Db.OeEntitySetAdapter? entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(typeInfo.ClrType);
                if (entitySetAdapter != null)
                {
                    EdmEntitySet entitySet = container.AddEntitySet(entitySetAdapter.EntitySetName, typeInfo.EdmType);
                    edmModel.SetEntitySetAdapter(entitySet, entitySetAdapter);
                    entitySets.Add(typeInfo.EdmType, entitySet);
                }
            }

            var manyToManyBuilder = new ManyToManyBuilder(edmModel, _metadataProvider, entityTypeInfos);
            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
                if (!typeInfo.IsRefModel)
                {
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                    {
                        EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                        EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];

                        if (fkeyInfo.DependentNavigationProperty == null)
                            principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, dependent);
                        else
                        {
                            dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);
                            if (fkeyInfo.EdmNavigationProperty.Partner != null)
                                principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                        }
                    }

                    manyToManyBuilder.Build(typeInfo);
                }

            foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations)
            {
                if (operationConfiguration.ReturnType == typeof(void))
                {
                    EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmAction);
                    container.AddActionImport(operationConfiguration.ImportName, edmAction);
                    edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction);
                }
                else
                {
                    EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmFunction);

                    if (edmFunction.IsBound)
                    {
                        if (operationConfiguration.MethodInfo == null)
                            throw new InvalidOperationException("Bound function operationConfiguration.MethodInfo must be not null");

                        edmModel.SetMethodInfo(edmFunction, operationConfiguration.MethodInfo);
                    }
                    else
                    {
                        container.AddFunctionImport(operationConfiguration.ImportName, edmFunction, edmFunction.EntitySetPath);
                        edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction);
                    }
                }
            }

            edmModel.AddElement(container);
            _dataAdapter.SetEdmModel(edmModel);
            foreach (IEdmModel refModel in refModels)
                edmModel.AddReferencedModel(refModel);

            return edmModel;
        }


        /// <summary>
        /// Writes the given object specified by the parameter graph as a part of an existing OData message using the given
        /// messageWriter and the writeContext.
        /// </summary>
        /// <param name="graph">The object to be written.</param>
        /// <param name="expectedType">The expected EDM type of the object represented by <paramref name="graph"/>.</param>
        /// <param name="writer">The <see cref="ODataWriter" /> to be used for writing.</param>
        /// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
        public virtual void WriteObjectInline(object graph, IEdmTypeReference expectedType, ODataWriter writer,
            ODataSerializerContext writeContext)
        {
            throw Error.NotSupported(SRResources.WriteObjectInlineNotSupported, GetType().Name);
        }

        /// <summary>
        /// Creates an <see cref="ODataValue"/> for the object represented by <paramref name="graph"/>.
        /// </summary>
        /// <param name="graph">The value of the <see cref="ODataValue"/> to be created.</param>
        /// <param name="expectedType">The expected EDM type of the object represented by <paramref name="graph"/>.</param>
        /// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param>
        /// <returns>The <see cref="ODataValue"/> created.</returns>
        public virtual ODataValue CreateODataValue(object graph, IEdmTypeReference expectedType, ODataSerializerContext writeContext)
        {
            throw Error.NotSupported(SRResources.CreateODataValueNotSupported, GetType().Name);
        }

        internal ODataProperty CreateProperty(object graph, IEdmTypeReference expectedType, string elementName,
            ODataSerializerContext writeContext)
        {
            Contract.Assert(elementName != null);
            return new ODataProperty
            {
                Name = elementName,
                Value = CreateODataValue(graph, expectedType, writeContext)
            };
        }

        internal List<ODataProperty> AppendDynamicProperties(object source, IEdmStructuredTypeReference structuredType,
            ODataSerializerContext writeContext, List<ODataProperty> declaredProperties)
        {
            Contract.Assert(source != null);
            Contract.Assert(structuredType != null);
            Contract.Assert(writeContext != null);
            Contract.Assert(writeContext.Model != null);

            PropertyInfo dynamicPropertyInfo = EdmLibHelpers.GetDynamicPropertyDictionary(
                structuredType.StructuredDefinition(), writeContext.Model);

            IEdmStructuredObject structuredObject = source as IEdmStructuredObject;
            object value;

            if (dynamicPropertyInfo == null || structuredObject == null ||
                !structuredObject.TryGetPropertyValue(dynamicPropertyInfo.Name, out value) || value == null)
            {
                return null;
            }

            IDictionary<string, object> dynamicPropertyDictionary = (IDictionary<string, object>)value;

            // Build a HashSet to store the declared property names.
            // It is used to make sure the dynamic property name is different from all declared property names.
            HashSet<string> declaredPropertyNameSet = new HashSet<string>(declaredProperties.Select(p => p.Name));
            List<ODataProperty> dynamicProperties = new List<ODataProperty>();
            foreach (KeyValuePair<string, object> dynamicProperty in dynamicPropertyDictionary)
            {
                if (String.IsNullOrEmpty(dynamicProperty.Key) || dynamicProperty.Value == null)
                {
                    continue; // skip the null object
                }

                if (declaredPropertyNameSet.Contains(dynamicProperty.Key))
                {
                    throw Error.InvalidOperation(SRResources.DynamicPropertyNameAlreadyUsedAsDeclaredPropertyName,
                        dynamicProperty.Key, structuredType.FullName());
                }

                IEdmTypeReference edmTypeReference = writeContext.GetEdmType(dynamicProperty.Value,
                    dynamicProperty.Value.GetType());
                if (edmTypeReference == null)
                {
                    throw Error.NotSupported(SRResources.TypeOfDynamicPropertyNotSupported,
                        dynamicProperty.Value.GetType().FullName, dynamicProperty.Key);
                }

                ODataEdmTypeSerializer propertySerializer = SerializerProvider.GetEdmTypeSerializer(edmTypeReference);
                if (propertySerializer == null)
                {
                    throw Error.NotSupported(SRResources.DynamicPropertyCannotBeSerialized, dynamicProperty.Key,
                        edmTypeReference.FullName());
                }

                dynamicProperties.Add(propertySerializer.CreateProperty(
                    dynamicProperty.Value, edmTypeReference, dynamicProperty.Key, writeContext));
            }

            return dynamicProperties;
        }

Microsoft.OData.Edm.EdmTypeReference : IEdmElement, IEdmTypeReference

Methods :

public Boolean get_IsNullable()
public IEdmType get_Definition()
public String ToString()
public Type GetType()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()