当前位置: 首页>>代码示例>>C#>>正文


C# ODataCollectionValue.SetAnnotation方法代码示例

本文整理汇总了C#中Microsoft.OData.Core.ODataCollectionValue.SetAnnotation方法的典型用法代码示例。如果您正苦于以下问题:C# ODataCollectionValue.SetAnnotation方法的具体用法?C# ODataCollectionValue.SetAnnotation怎么用?C# ODataCollectionValue.SetAnnotation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.OData.Core.ODataCollectionValue的用法示例。


在下文中一共展示了ODataCollectionValue.SetAnnotation方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddTypeNameAnnotationAsNeeded

        /// <summary>
        /// Adds the type name annotations required for proper json light serialization.
        /// </summary>
        /// <param name="value">The collection value for which the annotations have to be added.</param>
        /// <param name="metadataLevel">The OData metadata level of the response.</param>
        protected internal static void AddTypeNameAnnotationAsNeeded(ODataCollectionValue value, ODataMetadataLevel metadataLevel)
        {
            // ODataLib normally has the caller decide whether or not to serialize properties by leaving properties
            // null when values should not be serialized. The TypeName property is different and should always be
            // provided to ODataLib to enable model validation. A separate annotation is used to decide whether or not
            // to serialize the type name (a null value prevents serialization).

            // Note that this annotation should not be used for Atom or JSON verbose formats, as it will interfere with
            // the correct default behavior for those formats.

            Contract.Assert(value != null);

            // Only add an annotation if we want to override ODataLib's default type name serialization behavior.
            if (ShouldAddTypeNameAnnotation(metadataLevel))
            {
                string typeName;

                // Provide the type name to serialize (or null to force it not to serialize).
                if (ShouldSuppressTypeNameSerialization(metadataLevel))
                {
                    typeName = null;
                }
                else
                {
                    typeName = value.TypeName;
                }

                value.SetAnnotation<SerializationTypeNameAnnotation>(new SerializationTypeNameAnnotation
                {
                    TypeName = typeName
                });
            }
        }
开发者ID:quentez,项目名称:aspnetwebstack,代码行数:38,代码来源:ODataCollectionSerializer.cs

示例2: WriteInstanceAnnotation_ForCollectionShouldUseCollectionCodePath

        public void WriteInstanceAnnotation_ForCollectionShouldUseCollectionCodePath()
        {
            var collectionValue = new ODataCollectionValue() { TypeName = "Collection(String)" };
            collectionValue.SetAnnotation(new SerializationTypeNameAnnotation() { TypeName = null });
            const string term = "some.term";
            var verifierCalls = 0;

            this.jsonWriter.WriteNameVerifier = (name) =>
            {
                name.Should().Be("@" + term);
                verifierCalls++;
            };
            this.valueWriter.WriteCollectionVerifier = (value, reference, isTopLevelProperty, isInUri, isOpenProperty) =>
            {
                value.Should().Be(collectionValue);
                reference.Should().BeNull();
                isOpenProperty.Should().BeTrue();
                isTopLevelProperty.Should().BeFalse();
                isInUri.Should().BeFalse();
                verifierCalls.Should().Be(1);
                verifierCalls++;
            };
            this.jsonLightInstanceAnnotationWriter.WriteInstanceAnnotation(new ODataInstanceAnnotation(term, collectionValue));
            verifierCalls.Should().Be(2);
        }
开发者ID:rossjempson,项目名称:odata.net,代码行数:25,代码来源:JsonLightInstanceAnnotationWriterTests.cs

示例3: TypeNameShouldComeFromSerializationTypeNameAnnotationForCollectionValue

 public void TypeNameShouldComeFromSerializationTypeNameAnnotationForCollectionValue()
 {
     var stna = new SerializationTypeNameAnnotation() {TypeName = "FromSTNA"};
     var value = new ODataCollectionValue() {TypeName = "Collection(Edm.String)"};
     value.SetAnnotation(stna);
     this.typeNameOracle.GetValueTypeNameForWriting(value,
         EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(true)),
         EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetString(false)),
         /* isOpenProperty*/ false).Should().Be("FromSTNA");
 }
开发者ID:rossjempson,项目名称:odata.net,代码行数:10,代码来源:ODataWriterTypeNameUtilsTests.cs

示例4: CollectionValueSerializationTypeNameAnnotationTest


//.........这里部分代码省略.........
                    ExpectedExceptionInAtomForResponse = new object(),
                },
                new
                {
                    TypeName = EntityModelUtils.GetCollectionTypeName("Edm.Int32"),
                    SerializationTypeNameAnnotation = new SerializationTypeNameAnnotation() { TypeName = EntityModelUtils.GetCollectionTypeName("Edm.String") },
                    XmlTypeName = "<typeName>" + EntityModelUtils.GetCollectionTypeName("Edm.String") + "</typeName>",
                    JsonTypeName = "\"type\":\"" + EntityModelUtils.GetCollectionTypeName("Edm.String") + "\"",
                    JsonLightTypeName = "\"@odata.type\":\"#" + EntityModelUtils.GetCollectionTypeName("Edm.String") + "\"",
                    ExpectedExceptionInJsonLight = new object(),
                    ExpectedExceptionInAtom = new object(),
                    ExpectedExceptionInJsonLightForResponse = new object(),
                    ExpectedExceptionInAtomForResponse = new object(),
                },
            };
            #endregion test cases

            var testDescriptors = testCases.Select(tc =>
            {
                EdmModel model = new EdmModel();

                var owningEntityType = new EdmEntityType("TestNS", "OwningEntityType");
                owningEntityType.AddStructuralProperty("PropertyName", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetInt32(isNullable: false)));
                model.AddElement(owningEntityType);

                var container = new EdmEntityContainer("TestNS", "TestContainer");
                model.AddElement(container);

                ODataComplexValue complexValue = new ODataComplexValue();
                complexValue.TypeName = tc.TypeName;
                complexValue.Properties = new[] { new ODataProperty() { Name = "TestProperty", Value = "TestValue" } };
                if (tc.SerializationTypeNameAnnotation != null)
                {
                    complexValue.SetAnnotation(tc.SerializationTypeNameAnnotation);
                }

                ODataCollectionValue collection = new ODataCollectionValue();
                collection.TypeName = tc.TypeName;
                if (tc.SerializationTypeNameAnnotation != null)
                {
                    collection.SetAnnotation(tc.SerializationTypeNameAnnotation);
                }

                return new PayloadWriterTestDescriptor<ODataProperty>(
                    this.Settings,
                    new ODataProperty { Name = "PropertyName", Value = collection },
                    (testConfiguration) =>
                    {
                        if (testConfiguration.Format == ODataFormat.Atom)
                        {
                            if (tc.ExpectedExceptionInAtom is Exception)
                            {
                                return new WriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                                {
                                    ExpectedException = (Exception)tc.ExpectedExceptionInAtom
                                };
                            }

                            var exception = testConfiguration.IsRequest ? tc.ExpectedExceptionInAtom : tc.ExpectedExceptionInAtomForResponse;
                            if (exception is ExpectedException)
                            {
                                return new WriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                                {
                                    ExpectedException2 = (ExpectedException)exception
                                };
                            }
开发者ID:larsenjo,项目名称:odata.net,代码行数:67,代码来源:WriterPropertyTests.cs

示例5: BuildPropertyContextUriForCollectionPropertyValueWithNonNullAnnotation

 public void BuildPropertyContextUriForCollectionPropertyValueWithNonNullAnnotation()
 {
     ODataCollectionValue value = new ODataCollectionValue { TypeName = "FQNS.FromObject" };
     value.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = "FQNS.FromAnnotation" });
     var contextUri = this.CreatePropertyContextUri(value);
     contextUri.OriginalString.Should().Be(BuildExpectedContextUri("#FQNS.FromAnnotation"));
 }
开发者ID:rossjempson,项目名称:odata.net,代码行数:7,代码来源:ODataContextUriBuilderTests.cs

示例6: CreateODataCollection

        /// <summary>
        /// Creates and returns an ODataCollectionValue from the given value.
        /// </summary>
        /// <param name="collectionItemType">The type of the value.</param>
        /// <param name="propertyName">If the value is a property, then it represents the name of the property. Can be null, for non-property.</param>
        /// <param name="value">The value.</param>
        /// <param name="visitedComplexTypeObjects">Set of instances of complex types encountered in the hierarchy. Used to detect cycles.</param>
        /// <param name="isDynamicProperty">Whether this collection property is a dynamic property</param>
        /// <param name="setTypeAnnotation">If true, set the type annotation on ODataValue.</param>
        /// <returns>An ODataCollectionValue representing the given value.</returns>
        internal ODataCollectionValue CreateODataCollection(Type collectionItemType, string propertyName, object value, HashSet<object> visitedComplexTypeObjects, bool isDynamicProperty, bool setTypeAnnotation = true)
        {
            Debug.Assert(collectionItemType != null, "collectionItemType != null");

            WebUtil.ValidateCollection(collectionItemType, value, propertyName, isDynamicProperty);

            PrimitiveType ptype;
            bool isCollectionOfPrimitiveTypes = PrimitiveType.TryGetPrimitiveType(collectionItemType, out ptype);

            ODataCollectionValue collection = new ODataCollectionValue();
            IEnumerable enumerablePropertyValue = (IEnumerable)value;
            string collectionItemTypeName;
            string collectionTypeName;
            if (isCollectionOfPrimitiveTypes)
            {
                collectionItemTypeName = ClientConvert.GetEdmType(Nullable.GetUnderlyingType(collectionItemType) ?? collectionItemType);

                if (enumerablePropertyValue != null)
                {                    
                    collection.Items = Util.GetEnumerable(
                        enumerablePropertyValue,
                        (val) =>
                        {
                            if (val == null)
                            {
                                return null;
                            }

                            WebUtil.ValidatePrimitiveCollectionItem(val, propertyName, collectionItemType);
                            return ConvertPrimitiveValueToRecognizedODataType(val, collectionItemType);
                        });
                }

                // TypeName for primitives should be the EDM name since that's what we will be able to look up in the model
                collectionTypeName = collectionItemTypeName;
            }
            else
            {
                Type collectionItemTypeTmp = Nullable.GetUnderlyingType(collectionItemType) ?? collectionItemType;
                bool areEnumItems = collectionItemTypeTmp.IsEnum();

                // Note that the collectionItemTypeName will be null if the context does not have the ResolveName func.
                collectionItemTypeName = this.requestInfo.ResolveNameFromType(collectionItemType);

                if (enumerablePropertyValue != null)
                {
                    collection.Items = Util.GetEnumerable(
                        enumerablePropertyValue,
                        (val) =>
                        {
                            if (areEnumItems)
                            {
                                if (val == null)
                                {
                                    return new ODataEnumValue(null, collectionItemType.FullName) as ODataValue;
                                }

                                return new ODataEnumValue(ClientTypeUtil.GetEnumValuesString(val.ToString(), collectionItemTypeTmp), collectionItemType.FullName) as ODataValue;
                            }
                            else
                            {
                                if (val == null)
                                {
                                    return null;
                                }

                                WebUtil.ValidateComplexCollectionItem(val, propertyName, collectionItemType);
                                return this.CreateODataComplexValue(val.GetType(), val, propertyName, true /*isCollectionItem*/, visitedComplexTypeObjects)
                                     as ODataValue;
                            }
                        });
                }

                // TypeName for complex types needs to be the client type name (not the one we resolved above) since it will be looked up in the client model
                collectionTypeName = collectionItemType.FullName;
            }

            // Set the type name to use for client type lookups and validation. Because setting this value can cause validation to occur, we will
            // only do it for JSON Light, in order to avoid breaking changes with the WCF Data Services 5.0 release, since it was already shipped without this.
            if (!this.requestInfo.Format.UsingAtom)
            {
                collection.TypeName = GetCollectionName(collectionTypeName);
            }

            // Ideally, we should not set type annotation on collection value. 
            // To keep backward compatibility, we'll keep it in request body, but do not include it in url.
            if (setTypeAnnotation)
            {
                string wireTypeName = GetCollectionName(collectionItemTypeName);
                collection.SetAnnotation(new SerializationTypeNameAnnotation { TypeName = wireTypeName });
//.........这里部分代码省略.........
开发者ID:larsenjo,项目名称:odata.net,代码行数:101,代码来源:ODataPropertyConverter.cs


注:本文中的Microsoft.OData.Core.ODataCollectionValue.SetAnnotation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。