本文整理汇总了C#中IEdmTypeReference.AsComplex方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmTypeReference.AsComplex方法的具体用法?C# IEdmTypeReference.AsComplex怎么用?C# IEdmTypeReference.AsComplex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmTypeReference
的用法示例。
在下文中一共展示了IEdmTypeReference.AsComplex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadInline
/// <inheritdoc />
public sealed override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
{
if (readContext == null)
{
throw Error.ArgumentNull("readContext");
}
if (item == null)
{
return null;
}
ODataComplexValue complexValue = item as ODataComplexValue;
if (complexValue == null)
{
throw Error.Argument("item", SRResources.ArgumentMustBeOfType, typeof(ODataComplexValue).Name);
}
if (!edmType.IsComplex())
{
throw Error.Argument("edmType", SRResources.ArgumentMustBeOfType, EdmTypeKind.Complex);
}
// Recursion guard to avoid stack overflows
RuntimeHelpers.EnsureSufficientExecutionStack();
return ReadComplexValue(complexValue, edmType.AsComplex(), readContext);
}
示例2: 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);
}
}
示例3: CreateEdmTypeSerializer
/// <summary>
/// Creates a new instance of the <see cref="ODataEntrySerializer"/> for the given edm type.
/// </summary>
/// <param name="edmType">The <see cref="IEdmTypeReference"/>.</param>
/// <returns>The constructed <see cref="ODataEntrySerializer"/>.</returns>
public virtual ODataEntrySerializer 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;
}
}
示例4: 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;
}
示例5: CreateODataValue
/// <inheitdoc />
public sealed override ODataValue CreateODataValue(object graph, IEdmTypeReference expectedType,
ODataSerializerContext writeContext)
{
if (expectedType == null)
{
throw Error.ArgumentNull("expectedType");
}
if (!expectedType.IsComplex())
{
throw new SerializationException(
Error.Format(SRResources.CannotWriteType, GetType().Name, expectedType.FullName()));
}
return CreateODataComplexValue(graph, expectedType.AsComplex(), writeContext);
}
示例6: 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;
}
示例7: ValidateNullPropertyValue
/// <summary>
/// Validates that the expected property allows null value.
/// </summary>
/// <param name="expectedPropertyTypeReference">The expected property type or null if we don't have any.</param>
/// <param name="propertyName">The name of the property.</param>
/// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
/// <param name="model">The model to use to get the OData version.</param>
/// <param name="bypassValidation">Bypass the validation if it is true.</param>
internal static void ValidateNullPropertyValue(IEdmTypeReference expectedPropertyTypeReference, string propertyName, ODataWriterBehavior writerBehavior, IEdmModel model, bool bypassValidation = false)
{
Debug.Assert(writerBehavior != null, "writerBehavior != null");
Debug.Assert(model != null, "For null validation, model is required.");
if (bypassValidation)
{
return;
}
if (expectedPropertyTypeReference != null)
{
if (expectedPropertyTypeReference.IsNonEntityCollectionType())
{
throw new ODataException(Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(propertyName));
}
if (expectedPropertyTypeReference.IsODataPrimitiveTypeKind())
{
// WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
// See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
if (!expectedPropertyTypeReference.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
}
else if (expectedPropertyTypeReference.IsODataEnumTypeKind() && !expectedPropertyTypeReference.IsNullable)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
else if (expectedPropertyTypeReference.IsStream())
{
throw new ODataException(Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(propertyName));
}
else if (expectedPropertyTypeReference.IsODataComplexTypeKind())
{
if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
{
IEdmComplexTypeReference complexTypeReference = expectedPropertyTypeReference.AsComplex();
if (!complexTypeReference.IsNullable)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
}
}
}
}
示例8: GetPropertyValue
private object GetPropertyValue(IEdmTypeReference propertyType, object value)
{
if (value == null)
return value;
switch (propertyType.TypeKind())
{
case EdmTypeKind.Complex:
var complexTypeProperties = propertyType.AsComplex().StructuralProperties();
return new ODataComplexValue
{
TypeName = propertyType.FullName(),
Properties = value.ToDictionary()
.Where(val => complexTypeProperties.Any(p => p.Name == val.Key))
.Select(x => new ODataProperty
{
Name = x.Key,
Value = GetPropertyValue(complexTypeProperties, x.Key, x.Value),
})
};
case EdmTypeKind.Collection:
var collection = propertyType.AsCollection();
return new ODataCollectionValue()
{
TypeName = propertyType.FullName(),
Items = (value as IEnumerable<object>).Select(x => GetPropertyValue(collection.ElementType(), x)),
};
case EdmTypeKind.Primitive:
var mappedTypes = _typeMap.Where(x => x.Value == (propertyType.Definition as IEdmPrimitiveType).PrimitiveKind);
if (mappedTypes.Any())
{
foreach (var mappedType in mappedTypes)
{
object result;
if (Utils.TryConvert(value, mappedType.Key, out result))
return result;
}
throw new NotSupportedException(string.Format("Conversion is not supported from type {0} to OData type {1}", value.GetType(), propertyType));
}
return value;
case EdmTypeKind.Enum:
return new ODataEnumValue(value.ToString());
case EdmTypeKind.None:
if (CustomConverters.HasObjectConverter(value.GetType()))
{
return CustomConverters.Convert(value, value.GetType());
}
throw new NotSupportedException(string.Format("Conversion is not supported from type {0} to OData type {1}", value.GetType(), propertyType));
default:
return value;
}
}
示例9: ConvertComplexValue
private static object ConvertComplexValue(ODataComplexValue complexValue, ref IEdmTypeReference propertyType,
ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
{
IEdmComplexTypeReference edmComplexType;
if (propertyType == null)
{
// open complex property
Contract.Assert(!String.IsNullOrEmpty(complexValue.TypeName),
"ODataLib should have verified that open complex value has a type name since we provided metadata.");
IEdmModel model = readContext.Model;
IEdmType edmType = model.FindType(complexValue.TypeName);
Contract.Assert(edmType.TypeKind == EdmTypeKind.Complex, "ODataLib should have verified that complex value has a complex resource type.");
edmComplexType = new EdmComplexTypeReference(edmType as IEdmComplexType, isNullable: true);
propertyType = edmComplexType;
}
else
{
edmComplexType = propertyType.AsComplex();
}
ODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(edmComplexType);
return deserializer.ReadInline(complexValue, propertyType, readContext);
}
示例10: ResolveAndValidateTargetTypeStrictValidationDisabled
/// <summary>
/// Resolves the payload type versus the expected type and validates that such combination is allowed when the strict validation is disabled.
/// </summary>
/// <param name="expectedTypeKind">The expected type kind for the value.</param>
/// <param name="expectedTypeReference">The expected type reference, or null if no expected type is available.</param>
/// <param name="payloadType">The payload type, or null if the payload type was not specified, or it didn't resolve against the model.</param>
/// <returns>The target type reference to use for parsing the value.</returns>
private static IEdmTypeReference ResolveAndValidateTargetTypeStrictValidationDisabled(
EdmTypeKind expectedTypeKind,
IEdmTypeReference expectedTypeReference,
IEdmType payloadType)
{
// Lax validation logic
switch (expectedTypeKind)
{
case EdmTypeKind.Complex:
// if the expectedTypeKind is different from the payloadType.TypeKind the types are not related
// in any way. In that case we will just use the expected type because we are in lax mode.
if (payloadType != null && expectedTypeKind == payloadType.TypeKind)
{
// Verify if it's a derived complex type, in all other cases simply use the expected type.
VerifyComplexType(expectedTypeReference, payloadType, /* failIfNotRelated */ false);
if (EdmLibraryExtensions.IsAssignableFrom(expectedTypeReference.AsComplex().ComplexDefinition(), (IEdmComplexType)payloadType))
{
return payloadType.ToTypeReference(/*nullable*/ true);
}
}
break;
case EdmTypeKind.Entity:
// if the expectedTypeKind is different from the payloadType.TypeKind the types are not related
// in any way. In that case we will just use the expected type because we are in lax mode.
if (payloadType != null && expectedTypeKind == payloadType.TypeKind)
{
// If the type is assignable (equal or derived) we will use the payload type, since we want to allow derived entities
if (EdmLibraryExtensions.IsAssignableFrom(expectedTypeReference.AsEntity().EntityDefinition(), (IEdmEntityType)payloadType))
{
IEdmTypeReference payloadTypeReference = payloadType.ToTypeReference(/*nullable*/ true);
return payloadTypeReference;
}
}
break;
case EdmTypeKind.Collection:
// if the expectedTypeKind is different from the payloadType.TypeKind the types are not related
// in any way. In that case we will just use the expected type because we are in lax mode.
if (payloadType != null && expectedTypeKind == payloadType.TypeKind)
{
VerifyCollectionComplexItemType(expectedTypeReference, payloadType);
}
break;
case EdmTypeKind.Enum: // enum: no validation
break;
case EdmTypeKind.TypeDefinition: // type definition: no validation
break;
default:
throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ReaderValidationUtils_ResolveAndValidateTypeName_Strict_TypeKind));
}
// Either there's no payload type, in which case use the expected one, or the payload one and the expected one are equal.
return expectedTypeReference;
}
示例11: ValidateNullValueAllowed
/// <summary>
/// Validates that the specified <paramref name="expectedValueTypeReference"/> allows null values.
/// </summary>
/// <param name="expectedValueTypeReference">The expected type for the value, or null if no such type is available.</param>
/// <param name="validateNullValue">true to validate the null value; otherwise false.</param>
/// <param name="model">The model to use to get the OData-Version.</param>
/// <param name="propertyName">The name of the property whose value is being read, if applicable (used for error reporting).</param>
/// <param name="isDynamicProperty">Indicates whether the property is dynamic or unknown.</param>
private static void ValidateNullValueAllowed(IEdmTypeReference expectedValueTypeReference, bool validateNullValue, IEdmModel model, string propertyName, bool? isDynamicProperty)
{
Debug.Assert(model != null, "For null validation, model is required.");
if (validateNullValue && expectedValueTypeReference != null)
{
Debug.Assert(
expectedValueTypeReference.IsODataPrimitiveTypeKind() ||
expectedValueTypeReference.IsODataTypeDefinitionTypeKind() ||
expectedValueTypeReference.IsODataEnumTypeKind() ||
expectedValueTypeReference.IsODataComplexTypeKind() ||
expectedValueTypeReference.IsNonEntityCollectionType(),
"Only primitive, type definition, Enum, complex and collection types are supported by this method.");
if (expectedValueTypeReference.IsODataPrimitiveTypeKind())
{
// COMPAT 55: WCF DS allows null values for non-nullable properties
// For now ODataLib will always fail on null value when it is to be reported for a non-nullable property
// We should add a knob since WCF DS might need the different behavior.
if (!expectedValueTypeReference.IsNullable)
{
ThrowNullValueForNonNullableTypeException(expectedValueTypeReference, propertyName);
}
}
else if (expectedValueTypeReference.IsODataEnumTypeKind())
{
if (!expectedValueTypeReference.IsNullable)
{
ThrowNullValueForNonNullableTypeException(expectedValueTypeReference, propertyName);
}
}
else if (expectedValueTypeReference.IsNonEntityCollectionType())
{
if (isDynamicProperty != true)
{
ThrowNullValueForNonNullableTypeException(expectedValueTypeReference, propertyName);
}
}
else if (expectedValueTypeReference.IsODataComplexTypeKind())
{
if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
{
IEdmComplexTypeReference complexTypeReference = expectedValueTypeReference.AsComplex();
if (!complexTypeReference.IsNullable)
{
ThrowNullValueForNonNullableTypeException(expectedValueTypeReference, propertyName);
}
}
}
}
}
示例12: GetPropertyValue
private object GetPropertyValue(IEdmTypeReference propertyType, object value)
{
if (value == null)
return value;
switch (propertyType.TypeKind())
{
case EdmTypeKind.Complex:
return new ODataComplexValue()
{
TypeName = propertyType.FullName(),
Properties = value.ToDictionary().Select(x => new ODataProperty()
{
Name = x.Key,
Value = GetPropertyValue(propertyType.AsComplex().StructuralProperties(), x.Key, x.Value),
}),
};
case EdmTypeKind.Collection:
var collection = propertyType.AsCollection();
return new ODataCollectionValue()
{
TypeName = propertyType.FullName(),
Items = (value as IEnumerable<object>).Select(x => GetPropertyValue(collection.ElementType(), x)),
};
case EdmTypeKind.Primitive:
var mappedTypes = _typeMap.Where(x => x.Value == (propertyType.Definition as IEdmPrimitiveType).PrimitiveKind);
if (mappedTypes.Any())
{
foreach (var mappedType in mappedTypes)
{
object result;
if (Utils.TryConvert(value, mappedType.Key, out result))
return result;
}
throw new FormatException(string.Format("Unable to convert value of type {0} to OData type {1}", value.GetType(), propertyType));
}
return value;
case EdmTypeKind.Enum:
return new ODataEnumValue(value.ToString());
default:
return value;
}
}
示例13: 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);
}
示例14: ValidateNullValueAllowed
/// <summary>
/// Validates that the specified <paramref name="expectedValueTypeReference"/> allows null values.
/// </summary>
/// <param name="expectedValueTypeReference">The expected type for the value, or null if no such type is available.</param>
/// <param name="validateNullValue">true to validate the null value; otherwise false.</param>
/// <param name="model">The model to use to get the data service version.</param>
private static void ValidateNullValueAllowed(IEdmTypeReference expectedValueTypeReference, bool validateNullValue, IEdmModel model)
{
DebugUtils.CheckNoExternalCallers();
Debug.Assert(model != null, "For null validation, model is required.");
if (validateNullValue && expectedValueTypeReference != null)
{
Debug.Assert(
expectedValueTypeReference.IsODataPrimitiveTypeKind() ||
expectedValueTypeReference.IsODataComplexTypeKind() ||
expectedValueTypeReference.IsNonEntityODataCollectionTypeKind(),
"Only primitive, complex and collection types are supported by this method.");
if (expectedValueTypeReference.IsODataPrimitiveTypeKind())
{
if (!expectedValueTypeReference.IsNullable)
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
else if (expectedValueTypeReference.IsNonEntityODataCollectionTypeKind())
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
else if (expectedValueTypeReference.IsODataComplexTypeKind())
{
if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
{
IEdmComplexTypeReference complexTypeReference = expectedValueTypeReference.AsComplex();
if (!complexTypeReference.IsNullable)
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
}
}
}
示例15: ValidateNullValueAllowed
private static void ValidateNullValueAllowed(IEdmTypeReference expectedValueTypeReference, bool validateNullValue, IEdmModel model)
{
if (validateNullValue && (expectedValueTypeReference != null))
{
if (expectedValueTypeReference.IsODataPrimitiveTypeKind())
{
if (!expectedValueTypeReference.IsNullable)
{
throw new ODataException(Microsoft.Data.OData.Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
else
{
if (expectedValueTypeReference.IsNonEntityODataCollectionTypeKind())
{
throw new ODataException(Microsoft.Data.OData.Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
if ((expectedValueTypeReference.IsODataComplexTypeKind() && ValidationUtils.ShouldValidateComplexPropertyNullValue(model)) && !expectedValueTypeReference.AsComplex().IsNullable)
{
throw new ODataException(Microsoft.Data.OData.Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
}
}