在C#或Iterate EdmCollectionType中获取ODATA元素名称和数据类型

时间:2019-03-17 21:16:25

标签: c# odata metadata

我在这里获取实体名称,但是我无法在调试器中看到元素名称及其数据类型,如下图所示。

调试器图像显示了以“ entity.Type.ElementType”开头的元素数据类型的路径,但是Intellisense显示了“ ElementType”不是一个选项,尽管我在调试器图像中看到了它。

Code gets entity name but how to get element name and data type

Debugger path to element name and data type

我看到数据类型的路径是变量实体->类型->元素类型->定义->声明的属性->类型->定义->名称,但智能感知将无助于达到这一水平。

enter image description here

1 个答案:

答案 0 :(得分:0)

这是最终有效的方法:

 var request = WebRequest.CreateHttp(baseUrl + "$metadata");
            var settings = new ODataMessageReaderSettings() { MessageQuotas = new ODataMessageQuotas() { MaxReceivedMessageSize = 35000000 } };
            var metadataMessage = new ClientHttpResponseMessage((HttpWebResponse)request.GetResponse());

            using (var messageReader = new ODataMessageReader(metadataMessage, settings))
            {
                IEdmModel edmModel = messageReader.ReadMetadataDocument();

                foreach (var entity in edmModel.EntityContainer.EntitySets())
                {
                    string entityName = entity.Name; // entity name

                    IEdmCollectionType edmCollectionType = (IEdmCollectionType)entity.Type;
                    IEdmType edmType = edmCollectionType.ElementType.Definition;
                    IEdmStructuredType edmStructuredType = edmType as IEdmStructuredType;

                    foreach (IEdmProperty property in edmStructuredType.DeclaredProperties)
                    {
                        string propertyName = property.Name;

                        IEdmType edmType2 = property.Type.Definition;
                        var primitive = edmType2 as IEdmPrimitiveType;
                        string elementName = primitive.Name;


                    }
                }

            }