本文整理汇总了C#中IEdmTypeReference.IsCollection方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmTypeReference.IsCollection方法的具体用法?C# IEdmTypeReference.IsCollection怎么用?C# IEdmTypeReference.IsCollection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmTypeReference
的用法示例。
在下文中一共展示了IEdmTypeReference.IsCollection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertValue
internal static object ConvertValue(
object odataValue,
string parameterName,
Type expectedReturnType,
IEdmTypeReference propertyType,
IEdmModel model,
HttpRequestMessage request,
IServiceProvider serviceProvider)
{
var readContext = new ODataDeserializerContext
{
Model = model,
Request = request
};
// Enum logic can be removed after RestierEnumDeserializer extends ODataEnumDeserializer
var deserializerProvider = serviceProvider.GetService<ODataDeserializerProvider>();
var enumValue = odataValue as ODataEnumValue;
if (enumValue != null)
{
ODataEdmTypeDeserializer deserializer
= deserializerProvider.GetEdmTypeDeserializer(propertyType.AsEnum());
return deserializer.ReadInline(enumValue, propertyType, readContext);
}
var returnValue = ODataModelBinderConverter.Convert(
odataValue, propertyType, expectedReturnType, parameterName, readContext, serviceProvider);
if (!propertyType.IsCollection())
{
return returnValue;
}
return ConvertCollectionType(returnValue, expectedReturnType);
}
示例2: ReadInline
/// <inheritdoc />
public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
{
if (item == null)
{
return null;
}
if (edmType == null)
{
throw Error.ArgumentNull("edmType");
}
if (!edmType.IsCollection() || !edmType.AsCollection().ElementType().IsEntity())
{
throw Error.Argument("edmType", SRResources.TypeMustBeEntityCollection, edmType.ToTraceString(), typeof(IEdmEntityType).Name);
}
IEdmEntityTypeReference elementType = edmType.AsCollection().ElementType().AsEntity();
ODataFeedWithEntries feed = item as ODataFeedWithEntries;
if (feed == null)
{
throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataFeedWithEntries).Name);
}
// Recursion guard to avoid stack overflows
RuntimeHelpers.EnsureSufficientExecutionStack();
return ReadFeed(feed, elementType, readContext);
}
示例3: ReadInline
/// <inheritdoc />
public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
{
if (item == null)
{
return null;
}
if (edmType == null)
{
throw Error.ArgumentNull("edmType");
}
if (!edmType.IsCollection())
{
throw new SerializationException(
Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter)));
}
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
IEdmTypeReference elementType = collectionType.ElementType();
ODataCollectionValue collection = item as ODataCollectionValue;
if (collection == null)
{
throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name);
}
// Recursion guard to avoid stack overflows
RuntimeHelpers.EnsureSufficientExecutionStack();
return ReadCollectionValue(collection, elementType, readContext);
}
示例4: ReadFromMessageReader
protected override void ReadFromMessageReader(ODataMessageReader reader, IEdmTypeReference expectedType)
{
ODataProperty collectionProperty = reader.ReadProperty(expectedType);
Type type = Nullable.GetUnderlyingType(base.ExpectedType) ?? base.ExpectedType;
object obj2 = collectionProperty.Value;
if (expectedType.IsCollection())
{
object obj3;
Type collectionItemType = type;
Type implementationType = ClientTypeUtil.GetImplementationType(type, typeof(ICollection<>));
if (implementationType != null)
{
collectionItemType = implementationType.GetGenericArguments()[0];
obj3 = ODataMaterializer.CreateCollectionInstance(collectionProperty, type, base.ResponseInfo);
}
else
{
implementationType = typeof(ICollection<>).MakeGenericType(new Type[] { collectionItemType });
obj3 = ODataMaterializer.CreateCollectionInstance(collectionProperty, implementationType, base.ResponseInfo);
}
ODataMaterializer.ApplyCollectionDataValues(collectionProperty, base.ResponseInfo.IgnoreMissingProperties, base.ResponseInfo, obj3, collectionItemType, ODataMaterializer.GetAddToCollectionDelegate(implementationType));
this.currentValue = obj3;
}
else if (expectedType.IsComplex())
{
ODataComplexValue complexValue = obj2 as ODataComplexValue;
ODataMaterializer.MaterializeComplexTypeProperty(type, complexValue, base.ResponseInfo.IgnoreMissingProperties, base.ResponseInfo);
this.currentValue = complexValue.GetMaterializedValue();
}
else
{
ODataMaterializer.MaterializePrimitiveDataValue(base.ExpectedType, collectionProperty);
this.currentValue = collectionProperty.GetMaterializedValue();
}
}
示例5: ReadInline
/// <inheritdoc />
public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
{
if (item == null)
{
return null;
}
if (edmType == null)
{
throw Error.ArgumentNull("edmType");
}
if (!edmType.IsCollection())
{
throw new SerializationException(
Error.Format(SRResources.TypeCannotBeDeserialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter)));
}
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
IEdmTypeReference elementType = collectionType.ElementType();
ODataCollectionValue collection = item as ODataCollectionValue;
if (collection == null)
{
throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataCollectionValue).Name);
}
// Recursion guard to avoid stack overflows
RuntimeHelpers.EnsureSufficientExecutionStack();
IEnumerable result = ReadCollectionValue(collection, elementType, readContext);
if (result != null)
{
if (readContext.IsUntyped && elementType.IsComplex())
{
EdmComplexObjectCollection complexCollection = new EdmComplexObjectCollection(collectionType);
foreach (EdmComplexObject complexObject in result)
{
complexCollection.Add(complexObject);
}
return complexCollection;
}
else if (readContext.IsUntyped && elementType.IsEnum())
{
EdmEnumObjectCollection enumCollection = new EdmEnumObjectCollection(collectionType);
foreach (EdmEnumObject enumObject in result)
{
enumCollection.Add(enumObject);
}
return enumCollection;
}
else
{
Type elementClrType = EdmLibHelpers.GetClrType(elementType, readContext.Model);
IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType).Invoke(null, new object[] { result }) as IEnumerable;
return castedResult;
}
}
return null;
}
示例6: ReadWithExpectedType
/// <summary>
/// Reads a value from the message reader.
/// </summary>
/// <param name="expectedClientType">The expected client type being materialized into.</param>
/// <param name="expectedReaderType">The expected type for the underlying reader.</param>
protected override void ReadWithExpectedType(IEdmTypeReference expectedClientType, IEdmTypeReference expectedReaderType)
{
ODataProperty property = this.messageReader.ReadProperty(expectedReaderType);
Type underlyingExpectedType = Nullable.GetUnderlyingType(this.ExpectedType) ?? this.ExpectedType;
object propertyValue = property.Value;
if (expectedClientType.IsCollection())
{
Debug.Assert(WebUtil.IsCLRTypeCollection(underlyingExpectedType, this.MaterializerContext.Model) || (SingleResult.HasValue && !SingleResult.Value), "expected type must be collection or single result must be false");
// We are here for two cases:
// (1) Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T>
// (2) Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T
Type collectionItemType = this.ExpectedType;
Type collectionICollectionType = ClientTypeUtil.GetImplementationType(underlyingExpectedType, typeof(ICollection<>));
object collectionInstance;
if (collectionICollectionType != null)
{
// Case 1
collectionItemType = collectionICollectionType.GetGenericArguments()[0];
collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionPropertyInstance(property, underlyingExpectedType);
}
else
{
// Case 2
collectionICollectionType = typeof(ICollection<>).MakeGenericType(new Type[] { collectionItemType });
collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionPropertyInstance(property, collectionICollectionType);
}
bool isElementNullable = expectedClientType.AsCollection().ElementType().IsNullable;
this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues(
property,
collectionInstance,
collectionItemType,
ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType),
isElementNullable);
this.currentValue = collectionInstance;
}
else if (expectedClientType.IsComplex())
{
ODataComplexValue complexValue = propertyValue as ODataComplexValue;
Debug.Assert(this.MaterializerContext.Model.GetOrCreateEdmType(underlyingExpectedType).ToEdmTypeReference(false).IsComplex(), "expectedType must be complex type");
this.ComplexValueMaterializationPolicy.MaterializeComplexTypeProperty(underlyingExpectedType, complexValue);
this.currentValue = complexValue.GetMaterializedValue();
}
else if (expectedClientType.IsEnum())
{
this.currentValue = this.EnumValueMaterializationPolicy.MaterializeEnumTypeProperty(underlyingExpectedType, property);
}
else
{
Debug.Assert(this.MaterializerContext.Model.GetOrCreateEdmType(underlyingExpectedType).ToEdmTypeReference(false).IsPrimitive(), "expectedType must be primitive type");
this.currentValue = this.PrimitivePropertyConverter.ConvertPrimitiveValue(property.Value, this.ExpectedType);
}
}
示例7: ReadWithExpectedType
/// <summary>
/// Reads a value from the message reader.
/// </summary>
/// <param name="expectedClientType">The expected client type being materialized into.</param>
/// <param name="expectedReaderType">The expected type for the underlying reader.</param>
protected override void ReadWithExpectedType(IEdmTypeReference expectedClientType, IEdmTypeReference expectedReaderType)
{
if (!expectedClientType.IsCollection())
{
throw new DataServiceClientException(DSClient.Strings.AtomMaterializer_TypeShouldBeCollectionError(expectedClientType.FullName()));
}
Type underlyingExpectedType = Nullable.GetUnderlyingType(this.ExpectedType) ?? this.ExpectedType;
bool isClrCollection = WebUtil.IsCLRTypeCollection(underlyingExpectedType, this.MaterializerContext.Model);
Debug.Assert(isClrCollection || (SingleResult.HasValue && !SingleResult.Value), "expected type must be collection or single result must be false");
// We are here for two cases:
// (1) Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T>
// (2) Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T
Type collectionItemType = underlyingExpectedType;
Type collectionICollectionType = ClientTypeUtil.GetImplementationType(underlyingExpectedType, typeof(ICollection<>));
if (collectionICollectionType != null)
{
// Case 1 : Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T>
collectionItemType = collectionICollectionType.GetGenericArguments()[0];
}
else
{
// Case 2 : Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T
collectionICollectionType = typeof(ICollection<>).MakeGenericType(new Type[] { collectionItemType });
}
Type clrCollectionType = WebUtil.GetBackingTypeForCollectionProperty(collectionICollectionType, collectionItemType);
object collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionInstance((IEdmCollectionTypeReference)expectedClientType, clrCollectionType);
// Enumerator over our collection reader was created, then ApplyDataCollections was refactored to
// take an enumerable instead of a ODataCollectionValue. Enumerator is being used as a bridge
ODataCollectionReader collectionReader = messageReader.CreateODataCollectionReader();
NonEntityItemsEnumerable collectionEnumerable = new NonEntityItemsEnumerable(collectionReader);
bool isElementNullable = expectedClientType.AsCollection().ElementType().IsNullable;
this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues(
collectionEnumerable,
null /*wireTypeName*/,
collectionInstance,
collectionItemType,
ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType),
isElementNullable);
this.currentValue = collectionInstance;
}
示例8: GenerateTypeAttributes
private IEnumerable<XObject> GenerateTypeAttributes(IEdmTypeReference typeReference)
{
var typeAttributes = new List<XObject>();
if (typeReference.IsCollection())
{
var elementType = ((IEdmCollectionTypeReference)typeReference).ElementType();
typeAttributes.Add(new XAttribute("Type", "Collection(" + elementType.FullName() + ")" ));
AddFacetAttributes(elementType, typeAttributes);
}
else
{
typeAttributes.Add(new XAttribute("Type", typeReference.FullName()));
AddFacetAttributes(typeReference, typeAttributes);
}
return typeAttributes;
}
示例9: GetEntityType
private static IEdmEntityTypeReference GetEntityType(IEdmTypeReference feedType)
{
if (feedType.IsCollection())
{
IEdmTypeReference elementType = feedType.AsCollection().ElementType();
if (elementType.IsEntity())
{
return elementType.AsEntity();
}
}
string message = string.Format(
CultureInfo.InvariantCulture,
"{0} cannot write an object of type '{1}'.",
typeof(ODataDomainFeedSerializer).Name,
feedType.FullName());
throw new SerializationException(message);
}
示例10: ConvertFeedOrEntry
internal static object ConvertFeedOrEntry(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;
}
HttpRequestMessage request = readContext.Request;
ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings();
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(valueString)))
{
stream.Seek(0, SeekOrigin.Begin);
IODataRequestMessage oDataRequestMessage = new ODataMessageWrapper(stream, null,
request.GetODataContentIdMapping());
using (
ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage,
oDataReaderSettings, readContext.Model))
{
request.RegisterForDispose(oDataMessageReader);
if (edmTypeReference.IsCollection())
{
return ConvertFeed(oDataMessageReader, edmTypeReference, readContext);
}
else
{
return ConvertEntity(oDataMessageReader, edmTypeReference, readContext);
}
}
}
}
示例11: GetEntityType
private static IEdmEntityType GetEntityType(IEdmTypeReference type)
{
IEdmEntityType entityType = null;
if (type.IsEntity())
{
entityType = (IEdmEntityType)type.Definition;
}
else if (type.IsCollection())
{
type = ((IEdmCollectionType)type.Definition).ElementType;
if (type.IsEntity())
{
entityType = (IEdmEntityType)type.Definition;
}
}
return entityType;
}
示例12: ResolveType
private OdcmType ResolveType(IEdmTypeReference realizedType)
{
if (realizedType.IsCollection())
{
return ResolveType(realizedType.AsCollection().ElementType());
}
var realizedSchemaElement = (IEdmSchemaElement)realizedType.Definition;
return ResolveType(realizedSchemaElement.Name, realizedSchemaElement.Namespace);
}
示例13: ValidateMetadataType
/// <summary>
/// Validates that the (optional) <paramref name="typeReferenceFromMetadata"/> is the same as the (optional) <paramref name="typeReferenceFromValue"/>.
/// </summary>
/// <param name="typeReferenceFromMetadata">The (optional) type from the metadata definition (the expected type).</param>
/// <param name="typeReferenceFromValue">The (optional) type from the value (the actual type).</param>
/// <returns>The type as derived from the <paramref name="typeReferenceFromMetadata"/> and/or <paramref name="typeReferenceFromValue"/>.</returns>
private static IEdmTypeReference ValidateMetadataType(IEdmTypeReference typeReferenceFromMetadata, IEdmTypeReference typeReferenceFromValue)
{
if (typeReferenceFromMetadata == null)
{
// if we have no metadata information there is nothing to validate
return typeReferenceFromValue;
}
if (typeReferenceFromValue == null)
{
// derive the property type from the metadata
return typeReferenceFromMetadata;
}
Debug.Assert(typeReferenceFromValue.TypeKind() == typeReferenceFromMetadata.TypeKind(), "typeReferenceFromValue.TypeKind() == typeReferenceFromMetadata.TypeKind()");
// Make sure the types are the same
if (typeReferenceFromValue.IsODataPrimitiveTypeKind())
{
// Primitive types must match exactly except for nullability
ValidationUtils.ValidateMetadataPrimitiveType(typeReferenceFromMetadata, typeReferenceFromValue);
}
else if (typeReferenceFromMetadata.IsEntity())
{
ValidationUtils.ValidateEntityTypeIsAssignable((IEdmEntityTypeReference)typeReferenceFromMetadata, (IEdmEntityTypeReference)typeReferenceFromValue);
}
else if (typeReferenceFromMetadata.IsComplex())
{
ValidationUtils.ValidateComplexTypeIsAssignable(typeReferenceFromMetadata.Definition as IEdmComplexType, typeReferenceFromValue.Definition as IEdmComplexType);
}
else if (typeReferenceFromMetadata.IsCollection())
{
// Collection types must match exactly.
if (!(typeReferenceFromMetadata.Definition.IsElementTypeEquivalentTo(typeReferenceFromValue.Definition)))
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(typeReferenceFromValue.ODataFullName(), typeReferenceFromMetadata.ODataFullName()));
}
}
else
{
// For other types, compare their full type name.
if (typeReferenceFromMetadata.ODataFullName() != typeReferenceFromValue.ODataFullName())
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(typeReferenceFromValue.ODataFullName(), typeReferenceFromMetadata.ODataFullName()));
}
}
return typeReferenceFromValue;
}
示例14: GetEntityType
private static IEdmEntityTypeReference GetEntityType(IEdmTypeReference feedType)
{
if (feedType.IsCollection())
{
IEdmTypeReference elementType = feedType.AsCollection().ElementType();
if (elementType.IsEntity())
{
return elementType.AsEntity();
}
}
string message = Error.Format(SRResources.CannotWriteType, typeof(ODataFeedSerializer).Name, feedType.FullName());
throw new SerializationException(message);
}
示例15: GetEdmTypeSerializer
/// <summary>
/// Gets the serializer for the given EDM type reference.
/// </summary>
/// <param name="edmType">The EDM type reference involved in the serializer.</param>
/// <returns>The serializer instance.</returns>
public override ODataEdmTypeSerializer GetEdmTypeSerializer(IEdmTypeReference edmType)
{
if (edmType.IsEntity())
{
return this.entityTypeSerializer;
}
if (edmType.IsComplex())
{
return this.complexTypeSerializer;
}
if (edmType.IsPrimitive())
{
return this.primitiveSerializer;
}
if (edmType.IsEnum())
{
return this.enumSerializer;
}
if (edmType.IsCollection())
{
if (edmType.AsCollection().ElementType().IsEntity())
{
return this.feedSerializer;
}
return this.collectionSerializer;
}
return base.GetEdmTypeSerializer(edmType);
}