本文整理汇总了C#中IEdmTypeReference.AsPrimitive方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmTypeReference.AsPrimitive方法的具体用法?C# IEdmTypeReference.AsPrimitive怎么用?C# IEdmTypeReference.AsPrimitive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmTypeReference
的用法示例。
在下文中一共展示了IEdmTypeReference.AsPrimitive方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDeserializer
protected override ODataEntryDeserializer CreateDeserializer(IEdmTypeReference edmType)
{
if (edmType != null)
{
switch (edmType.TypeKind())
{
case EdmTypeKind.Entity:
return new ODataEntityDeserializer(edmType.AsEntity(), this);
case EdmTypeKind.Primitive:
return new ODataPrimitiveDeserializer(edmType.AsPrimitive());
case EdmTypeKind.Complex:
return new ODataComplexTypeDeserializer(edmType.AsComplex(), this);
case EdmTypeKind.Collection:
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
if (collectionType.ElementType().IsEntity())
{
return new ODataFeedDeserializer(collectionType, this);
}
else
{
return new ODataCollectionDeserializer(collectionType, this);
}
}
}
return null;
}
示例2: CreateEdmTypeSerializer
/// <summary>
/// Creates a new instance of the <see cref="ODataEdmTypeSerializer"/> for the given edm type.
/// </summary>
/// <param name="edmType">The <see cref="IEdmTypeReference"/>.</param>
/// <returns>The constructed <see cref="ODataEdmTypeSerializer"/>.</returns>
public virtual ODataEdmTypeSerializer CreateEdmTypeSerializer(IEdmTypeReference edmType)
{
if (edmType == null)
{
throw Error.ArgumentNull("edmType");
}
switch (edmType.TypeKind())
{
case EdmTypeKind.Primitive:
return new ODataPrimitiveSerializer(edmType.AsPrimitive());
case EdmTypeKind.Collection:
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
if (collectionType.ElementType().IsEntity())
{
return new ODataFeedSerializer(collectionType, this);
}
else
{
return new ODataCollectionSerializer(collectionType, this);
}
case EdmTypeKind.Complex:
return new ODataComplexTypeSerializer(edmType.AsComplex(), this);
case EdmTypeKind.Entity:
return new ODataEntityTypeSerializer(edmType.AsEntity(), this);
default:
return null;
}
}
示例3: CreateEdmTypeSerializer
public override ODataSerializer CreateEdmTypeSerializer(IEdmTypeReference edmType)
{
if (edmType == null)
{
throw Error.ArgumentNull("edmType");
}
switch (edmType.TypeKind())
{
case EdmTypeKind.Primitive:
return new ODataPrimitiveSerializer(edmType.AsPrimitive());
case EdmTypeKind.Collection:
IEdmCollectionTypeReference collectionType = edmType.AsCollection();
if (collectionType.ElementType().IsEntity())
{
return new ODataFeedSerializer(collectionType, this);
}
else
{
return new ODataCollectionSerializer(collectionType, this);
}
case EdmTypeKind.Complex:
return new ODataComplexTypeSerializer(edmType.AsComplex(), this);
case EdmTypeKind.Entity:
return new ODataEntityTypeSerializer(edmType.AsEntity(), this);
default:
throw Error.InvalidOperation(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter).Name);
}
}
示例4: CreateODataValue
/// <inheritdoc/>
public sealed override ODataValue CreateODataValue(object graph, IEdmTypeReference expectedType, ODataSerializerContext writeContext)
{
if (!expectedType.IsPrimitive())
{
throw Error.InvalidOperation(SRResources.CannotWriteType, typeof(ODataPrimitiveSerializer), expectedType.FullName());
}
ODataPrimitiveValue value = CreateODataPrimitiveValue(graph, expectedType.AsPrimitive(), writeContext);
if (value == null)
{
return new ODataNullValue();
}
return value;
}
示例5: CreateDeserializer
protected override ODataEntryDeserializer CreateDeserializer(IEdmTypeReference edmType)
{
if (edmType != null)
{
switch (edmType.TypeKind())
{
case EdmTypeKind.Entity:
return new ODataEntityDeserializer(edmType.AsEntity(), this);
case EdmTypeKind.Primitive:
return new ODataRawValueDeserializer(edmType.AsPrimitive());
case EdmTypeKind.Complex:
return new ODataComplexTypeDeserializer(edmType.AsComplex(), this);
}
}
return null;
}
示例6: GetValueTypeNameForWriting
/// <summary>
/// Determines the type name to write to the payload. Json Light type names are only written into the payload for open properties
/// or if the payload type name is more derived than the model type name.
/// </summary>
/// <param name="value">The ODataValue whose type name is to be written.</param>
/// <param name="typeReferenceFromMetadata">The type as expected by the model.</param>
/// <param name="typeReferenceFromValue">The type resolved from the value.</param>
/// <param name="isOpenProperty">true if the type name belongs to an open property, false otherwise.</param>
/// <returns>Type name to write to the payload, or null if no type should be written.</returns>
internal override string GetValueTypeNameForWriting(
ODataValue value,
IEdmTypeReference typeReferenceFromMetadata,
IEdmTypeReference typeReferenceFromValue,
bool isOpenProperty)
{
SerializationTypeNameAnnotation typeNameAnnotation = value.GetAnnotation<SerializationTypeNameAnnotation>();
if (typeNameAnnotation != null)
{
return typeNameAnnotation.TypeName;
}
// Do not write type name when the type is native json type.
if (typeReferenceFromValue != null
&& typeReferenceFromValue.IsPrimitive()
&& JsonSharedUtils.ValueTypeMatchesJsonType((ODataPrimitiveValue)value, typeReferenceFromValue.AsPrimitive()))
{
return null;
}
return GetTypeNameFromValue(value);
}
示例7: ConvertValue
/// <summary>
/// Converts an OData value into the corresponding <see cref="IEdmDelayedValue"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="type">The <see cref="IEdmTypeReference"/> of the value or null if no type reference is available.</param>
/// <returns>An <see cref="IEdmDelayedValue"/> implementation of the <paramref name="value"/>.</returns>
internal static IEdmDelayedValue ConvertValue(object value, IEdmTypeReference type)
{
if (value == null)
{
return type == null ? ODataEdmNullValue.UntypedInstance : new ODataEdmNullValue(type);
}
ODataComplexValue complexValue = value as ODataComplexValue;
if (complexValue != null)
{
return new ODataEdmStructuredValue(complexValue);
}
ODataCollectionValue collectionValue = value as ODataCollectionValue;
if (collectionValue != null)
{
return new ODataEdmCollectionValue(collectionValue);
}
// If the property value is not null, a complex value or a collection value,
// it has to be a primitive value
return EdmValueUtils.ConvertPrimitiveValue(value, type == null ? null : type.AsPrimitive());
}
示例8: GetValueTypeNameForWriting
/// <summary>
/// Determines the type name to write to the payload. Json Light type names are only written into the payload for open properties
/// or if the payload type name is more derived than the model type name.
/// </summary>
/// <param name="value">The ODataValue whose type name is to be written.</param>
/// <param name="typeReferenceFromMetadata">The type as expected by the model.</param>
/// <param name="typeReferenceFromValue">The type resolved from the value.</param>
/// <param name="isOpenProperty">true if the type name belongs to an open property, false otherwise.</param>
/// <returns>Type name to write to the payload, or null if no type should be written.</returns>
internal override string GetValueTypeNameForWriting(
ODataValue value,
IEdmTypeReference typeReferenceFromMetadata,
IEdmTypeReference typeReferenceFromValue,
bool isOpenProperty)
{
SerializationTypeNameAnnotation typeNameAnnotation = value.GetAnnotation<SerializationTypeNameAnnotation>();
if (typeNameAnnotation != null)
{
return typeNameAnnotation.TypeName;
}
if (typeReferenceFromValue != null)
{
// Write type name when the type in the payload is more derived than the type from metadata.
if (typeReferenceFromMetadata != null && typeReferenceFromMetadata.Definition.AsActualType().ODataFullName() != typeReferenceFromValue.ODataFullName())
{
return typeReferenceFromValue.ODataFullName();
}
// Note: When writing derived complexType value in a payload, we don't have the expected type.
// So always write @odata.type for top-level derived complextype.
if (typeReferenceFromMetadata == null && typeReferenceFromValue.IsComplex())
{
if ((typeReferenceFromValue as IEdmComplexTypeReference).ComplexDefinition().BaseType != null)
{
return typeReferenceFromValue.ODataFullName();
}
}
// Do not write type name when the type is native json type.
if (typeReferenceFromValue.IsPrimitive() && JsonSharedUtils.ValueTypeMatchesJsonType((ODataPrimitiveValue)value, typeReferenceFromValue.AsPrimitive()))
{
return null;
}
}
if (!isOpenProperty)
{
// Do not write type name for non-open properties since we expect the reader to have an expected type (via API or context URI) and thus not need it.
return null;
}
return GetTypeNameFromValue(value);
}
示例9: GetClrTypeForUntypedDelta
internal static Type GetClrTypeForUntypedDelta(IEdmTypeReference edmType)
{
Contract.Assert(edmType != null);
switch (edmType.TypeKind())
{
case EdmTypeKind.Primitive:
return EdmLibHelpers.GetClrType(edmType.AsPrimitive(), EdmCoreModel.Instance);
case EdmTypeKind.Complex:
return typeof(EdmComplexObject);
case EdmTypeKind.Entity:
return typeof(EdmEntityObject);
case EdmTypeKind.Enum:
return typeof(EdmEnumObject);
case EdmTypeKind.Collection:
IEdmTypeReference elementType = edmType.AsCollection().ElementType();
if (elementType.IsPrimitive())
{
Type elementClrType = GetClrTypeForUntypedDelta(elementType);
return typeof(List<>).MakeGenericType(elementClrType);
}
else if (elementType.IsComplex())
{
return typeof(EdmComplexObjectCollection);
}
else if (elementType.IsEntity())
{
return typeof(EdmEntityObjectCollection);
}
else if (elementType.IsEnum())
{
return typeof(EdmEnumObjectCollection);
}
break;
}
throw Error.InvalidOperation(SRResources.UnsupportedEdmType, edmType.ToTraceString(), edmType.TypeKind());
}
示例10: ConvertToTaupoDataType
private DataType ConvertToTaupoDataType(IEdmTypeReference edmTypeReference)
{
EdmTypeKind kind = edmTypeReference.TypeKind();
if (kind == EdmTypeKind.Collection)
{
var elementEdmTypeReference = edmTypeReference.AsCollection().ElementType();
return DataTypes.CollectionType
.WithElementDataType(this.ConvertToTaupoDataType(elementEdmTypeReference))
.Nullable(edmTypeReference.IsNullable);
}
else if (kind == EdmTypeKind.Complex)
{
var complexEdmTypeDefinition = edmTypeReference.AsComplex().ComplexDefinition();
return DataTypes.ComplexType
.WithName(complexEdmTypeDefinition.Namespace, complexEdmTypeDefinition.Name)
.Nullable(edmTypeReference.IsNullable);
}
else if (kind == EdmTypeKind.Entity)
{
var entityEdmTypeDefinition = edmTypeReference.AsEntity().EntityDefinition();
return DataTypes.EntityType
.WithName(entityEdmTypeDefinition.Namespace, entityEdmTypeDefinition.Name)
.Nullable(edmTypeReference.IsNullable);
}
else if (kind == EdmTypeKind.EntityReference)
{
var entityEdmTypeDefinition = edmTypeReference.AsEntityReference().EntityType();
return DataTypes.ReferenceType
.WithEntityType(new EntityTypeReference(entityEdmTypeDefinition.Namespace, entityEdmTypeDefinition.Name))
.Nullable(edmTypeReference.IsNullable);
}
else if (kind == EdmTypeKind.Primitive)
{
return EdmToTaupoPrimitiveDataTypeConverter.ConvertToTaupoPrimitiveDataType(edmTypeReference.AsPrimitive());
}
else if (kind == EdmTypeKind.Enum)
{
var enumTypeDefinition = edmTypeReference.AsEnum().EnumDefinition();
return DataTypes.EnumType.WithName(enumTypeDefinition.Namespace, enumTypeDefinition.Name);
}
throw new TaupoInvalidOperationException("unexpected Edm Type Kind: " + kind);
}
示例11: ConvertToTypeIfNeeded
/// <summary>
/// If the source node is not of the specified type, then we check if type promotion is possible and inject a convert node.
/// If the source node is the same type as the target type (or if the target type is null), we just return the source node as is.
/// </summary>
/// <param name="source">The source node to apply the convertion to.</param>
/// <param name="targetTypeReference">The target primitive type. May be null - this method will do nothing in that case.</param>
/// <returns>The converted query node, or the original source node unchanged.</returns>
internal static SingleValueNode ConvertToTypeIfNeeded(SingleValueNode source, IEdmTypeReference targetTypeReference)
{
Debug.Assert(source != null, "source != null");
if (targetTypeReference == null)
{
return source;
}
if (source.TypeReference != null)
{
if (source.TypeReference.IsEquivalentTo(targetTypeReference))
{
// For source is type definition, if source's underlying type == target type.
// We create a conversion node from source to its underlying type (target type)
// so that the service can convert value of source clr type to underlying clr type.
if (source.TypeReference.IsTypeDefinition())
{
return new ConvertNode(source, targetTypeReference);
}
return source;
}
if (!TypePromotionUtils.CanConvertTo(source, source.TypeReference, targetTypeReference))
{
throw new ODataException(ODataErrorStrings.MetadataBinder_CannotConvertToType(source.TypeReference.FullName(), targetTypeReference.FullName()));
}
else
{
ConstantNode constantNode = source as ConstantNode;
if (source.TypeReference.IsEnum() && constantNode != null)
{
return new ConstantNode(constantNode.Value, ODataUriUtils.ConvertToUriLiteral(constantNode.Value, ODataVersion.V4), targetTypeReference);
}
object originalPrimitiveValue;
if (MetadataUtilsCommon.TryGetConstantNodePrimitiveDate(source, out originalPrimitiveValue) && (originalPrimitiveValue != null))
{
// DateTimeOffset -> Date when (target is Date) and (originalValue match Date format) and (ConstantNode)
object targetPrimitiveValue = ODataUriConversionUtils.CoerceTemporalType(originalPrimitiveValue, targetTypeReference.AsPrimitive().Definition as IEdmPrimitiveType);
if (targetPrimitiveValue != null)
{
if (string.IsNullOrEmpty(constantNode.LiteralText))
{
return new ConstantNode(targetPrimitiveValue);
}
return new ConstantNode(targetPrimitiveValue, constantNode.LiteralText, targetTypeReference);
}
}
if (MetadataUtilsCommon.TryGetConstantNodePrimitiveLDMF(source, out originalPrimitiveValue) && (originalPrimitiveValue != null))
{
// L F D M types : directly create a ConvertNode.
// 1. NodeToExpressionTranslator.cs won't allow implicitly converting single/double to decimal, which should be done here at Node tree level.
// 2. And prevent losing precision in float -> double, e.g. (double)1.234f => 1.2339999675750732d not 1.234d
object targetPrimitiveValue = ODataUriConversionUtils.CoerceNumericType(originalPrimitiveValue, targetTypeReference.AsPrimitive().Definition as IEdmPrimitiveType);
if (string.IsNullOrEmpty(constantNode.LiteralText))
{
return new ConstantNode(targetPrimitiveValue);
}
return new ConstantNode(targetPrimitiveValue, constantNode.LiteralText);
}
else
{
// other type conversion : ConvertNode
return new ConvertNode(source, targetTypeReference);
}
}
}
else
{
// If the source doesn't have a type (possibly an open property), then it's possible to convert it
// cause we don't know for sure.
return new ConvertNode(source, targetTypeReference);
}
}
示例12: TestTypeMatch
private static bool TestTypeMatch(this IEdmTypeReference expressionType, IEdmTypeReference assertedType, EdmLocation location, out IEnumerable<EdmError> discoveredErrors)
{
if (expressionType.TestNullabilityMatch(assertedType, location, out discoveredErrors))
{
if (expressionType.TypeKind() == EdmTypeKind.None || expressionType.IsBad())
{
discoveredErrors = Enumerable.Empty<EdmError>();
return true;
}
else
{
if (!expressionType.IsPrimitive() || !assertedType.IsPrimitive())
{
if (!expressionType.Definition.IsEquivalentTo(assertedType.Definition))
{
EdmError[] edmError = new EdmError[1];
edmError[0] = new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
discoveredErrors = edmError;
return false;
}
}
else
{
if (!expressionType.PrimitiveKind().PromotesTo(assertedType.AsPrimitive().PrimitiveKind()))
{
EdmError[] edmErrorArray = new EdmError[1];
edmErrorArray[0] = new EdmError(location, EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType(expressionType.FullName(), assertedType.FullName()));
discoveredErrors = edmErrorArray;
return false;
}
}
discoveredErrors = Enumerable.Empty<EdmError>();
return true;
}
}
else
{
return false;
}
}
示例13: TestTypeMatch
private static bool TestTypeMatch(this IEdmTypeReference expressionType, IEdmTypeReference assertedType, EdmLocation location, out IEnumerable<EdmError> discoveredErrors)
{
if (!TestNullabilityMatch(expressionType, assertedType, location, out discoveredErrors))
{
return false;
}
// A bad type matches anything (so as to avoid generating spurious errors).
if (expressionType.TypeKind() == EdmTypeKind.None || expressionType.IsBad())
{
discoveredErrors = Enumerable.Empty<EdmError>();
return true;
}
if (expressionType.IsPrimitive() && assertedType.IsPrimitive())
{
if (!expressionType.PrimitiveKind().PromotesTo(assertedType.AsPrimitive().PrimitiveKind()))
{
discoveredErrors = new EdmError[] { new EdmError(location, EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType(expressionType.FullName(), assertedType.FullName())) };
return false;
}
}
else
{
if (!expressionType.Definition.IsEquivalentTo(assertedType.Definition))
{
discoveredErrors = new EdmError[] { new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType) };
return false;
}
}
discoveredErrors = Enumerable.Empty<EdmError>();
return true;
}