本文整理汇总了C#中IEdmModel.SetAnnotationValue方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmModel.SetAnnotationValue方法的具体用法?C# IEdmModel.SetAnnotationValue怎么用?C# IEdmModel.SetAnnotationValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmModel
的用法示例。
在下文中一共展示了IEdmModel.SetAnnotationValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ODataComplexTypeSerializerTests
public ODataComplexTypeSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_address = new Address()
{
Street = "One Microsoft Way",
City = "Redmond",
State = "Washington",
Country = "United States",
ZipCode = "98052"
};
_addressType = _model.FindDeclaredType("Default.Address") as IEdmComplexType;
_model.SetAnnotationValue(_addressType, new ClrTypeAnnotation(typeof(Address)));
_addressTypeRef = _addressType.ToEdmTypeReference(isNullable: false).AsComplex();
var cnAddressType = _model.FindDeclaredType("Default.CnAddress") as IEdmComplexType;
_model.SetAnnotationValue(cnAddressType, new ClrTypeAnnotation(typeof(CnAddress)));
var usAddressType = _model.FindDeclaredType("Default.UsAddress") as IEdmComplexType;
_model.SetAnnotationValue(usAddressType, new ClrTypeAnnotation(typeof(UsAddress)));
_locationType = _model.FindDeclaredType("Default.Location") as IEdmComplexType;
_model.SetAnnotationValue(_locationType, new ClrTypeAnnotation(typeof(Location)));
_locationTypeRef = _locationType.ToEdmTypeReference(isNullable: false).AsComplex();
ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider();
_serializer = new ODataComplexTypeSerializer(serializerProvider);
TimeZoneInfoHelper.TimeZone = null;
}
示例2: Annotate
public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
{
if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
{
edmModel.SetAnnotationValue(edmElement, AnnotationNamespace, AnnotationAttribute, metadataElement.Identity.Id);
var clrType = _clrTypeProvider.Get(metadataElement.Identity);
if (clrType != null)
{
edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
}
}
}
示例3: ODataDeltaFeedSerializerTests
public ODataDeltaFeedSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_customerSet = _model.EntityContainer.FindEntitySet("Customers");
_model.SetAnnotationValue(_customerSet.EntityType(), new ClrTypeAnnotation(typeof(Customer)));
_path = new ODataPath(new EntitySetPathSegment(_customerSet));
_customers = new[] {
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
},
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 42,
}
};
_deltaFeedCustomers = new EdmChangedObjectCollection(_customerSet.EntityType());
EdmDeltaEntityObject newCustomer = new EdmDeltaEntityObject(_customerSet.EntityType());
newCustomer.TrySetPropertyValue("ID", 10);
newCustomer.TrySetPropertyValue("FirstName", "Foo");
_deltaFeedCustomers.Add(newCustomer);
_customersType = _model.GetEdmTypeReference(typeof(Customer[])).AsCollection();
_writeContext = new ODataSerializerContext() { NavigationSource = _customerSet, Model = _model, Path = _path };
}
示例4: ODataEntityTypeSerializerTests
public ODataEntityTypeSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Customer"), new ClrTypeAnnotation(typeof(Customer)));
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Order"), new ClrTypeAnnotation(typeof(Order)));
_customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
_customer = new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
};
ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(_model);
_serializer = new ODataEntityTypeSerializer(new EdmEntityTypeReference(_customerSet.ElementType, isNullable: false), serializerProvider);
_urlHelper = new Mock<UrlHelper>(new HttpRequestMessage()).Object;
_writeContext = new ODataSerializerContext() { EntitySet = _customerSet, UrlHelper = _urlHelper };
}
示例5: Annotate
public void Annotate(IMetadataElement metadataElement, IEdmElement edmElement, IEdmModel edmModel)
{
if (metadataElement is EntityElement || metadataElement is EnumTypeElement)
{
var typeName = metadataElement.Identity.Id.Segments.Last();
var clrType = _clrTypes.SingleOrDefault(x => x.Name.Equals(typeName, StringComparison.OrdinalIgnoreCase));
if (clrType != null)
{
edmModel.SetAnnotationValue(edmElement, new ClrTypeAnnotation(clrType));
}
}
}
示例6: ODataEntityTypeSerializerTests
public ODataEntityTypeSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Customer"), new ClrTypeAnnotation(typeof(Customer)));
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Order"), new ClrTypeAnnotation(typeof(Order)));
_customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
_customer = new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
};
_serializerProvider = new DefaultODataSerializerProvider();
_customerType = _model.GetEdmTypeReference(typeof(Customer)).AsEntity();
_serializer = new ODataEntityTypeSerializer(_serializerProvider);
_writeContext = new ODataSerializerContext() { EntitySet = _customerSet, Model = _model };
_entityInstanceContext = new EntityInstanceContext(_writeContext, _customerSet.ElementType.AsReference(), _customer);
}
示例7: ODataEntityTypeSerializerTests
public ODataEntityTypeSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Customer"), new ClrTypeAnnotation(typeof(Customer)));
_model.SetAnnotationValue<ClrTypeAnnotation>(_model.FindType("Default.Order"), new ClrTypeAnnotation(typeof(Order)));
_model.SetAnnotationValue(
_model.FindType("Default.SpecialCustomer"),
new ClrTypeAnnotation(typeof(SpecialCustomer)));
_model.SetAnnotationValue(
_model.FindType("Default.SpecialOrder"),
new ClrTypeAnnotation(typeof(SpecialOrder)));
_customerSet = _model.EntityContainer.FindEntitySet("Customers");
_customer = new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
};
_orderSet = _model.EntityContainer.FindEntitySet("Orders");
_order = new Order
{
ID = 20,
};
_serializerProvider = new DefaultODataSerializerProvider();
_customerType = _model.GetEdmTypeReference(typeof(Customer)).AsEntity();
_orderType = _model.GetEdmTypeReference(typeof(Order)).AsEntity();
_specialCustomerType = _model.GetEdmTypeReference(typeof(SpecialCustomer)).AsEntity();
_specialOrderType = _model.GetEdmTypeReference(typeof(SpecialOrder)).AsEntity();
_serializer = new ODataEntityTypeSerializer(_serializerProvider);
_path = new ODataPath(new EntitySetPathSegment(_customerSet));
_writeContext = new ODataSerializerContext() { NavigationSource = _customerSet, Model = _model, Path = _path };
_entityInstanceContext = new EntityInstanceContext(_writeContext, _customerSet.EntityType().AsReference(), _customer);
}
示例8: GetPropertyInfoTypeAnnotation
/// <summary>
/// Gets the property info annotation for the specified type or creates a new one if it doesn't exist.
/// </summary>
/// <param name="structuredType">The type to get the annotation for.</param>
/// <param name="model">The model containing annotations.</param>
/// <returns>The property info annotation.</returns>
internal static PropertyInfoTypeAnnotation GetPropertyInfoTypeAnnotation(IEdmStructuredType structuredType, IEdmModel model)
{
DebugUtils.CheckNoExternalCallers();
Debug.Assert(structuredType != null, "structuredType != null");
Debug.Assert(model != null, "model != null");
PropertyInfoTypeAnnotation propertyInfoTypeAnnotation = model.GetAnnotationValue<PropertyInfoTypeAnnotation>(structuredType);
if (propertyInfoTypeAnnotation == null)
{
propertyInfoTypeAnnotation = new PropertyInfoTypeAnnotation();
model.SetAnnotationValue(structuredType, propertyInfoTypeAnnotation);
}
return propertyInfoTypeAnnotation;
}
示例9: SetCanReflectOnInstanceTypeProperty
/// <summary>
/// Sets whether reflection over the property is allowed or not.
/// </summary>
/// <param name="property">The property to check.</param>
/// <param name="model">The model containing annotations.</param>
/// <param name="canReflect">true if reflection over the property is allowed; otherwise false.</param>
public static void SetCanReflectOnInstanceTypeProperty(this IEdmProperty property, IEdmModel model, bool canReflect)
{
ExceptionUtils.CheckArgumentNotNull(property, "property");
ExceptionUtils.CheckArgumentNotNull(model, "model");
ODataQueryEdmPropertyAnnotation annotation = model.GetAnnotationValue<ODataQueryEdmPropertyAnnotation>(property);
if (annotation == null)
{
if (canReflect)
{
annotation = new ODataQueryEdmPropertyAnnotation
{
CanReflectOnProperty = true
};
model.SetAnnotationValue(property, annotation);
}
}
else
{
annotation.CanReflectOnProperty = canReflect;
}
}
示例10: ODataFeedSerializerTests
public ODataFeedSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_customerSet = _model.EntityContainer.FindEntitySet("Customers");
_model.SetAnnotationValue(_customerSet.EntityType(), new ClrTypeAnnotation(typeof(Customer)));
_customers = new[] {
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
},
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 42,
}
};
_customersType = _model.GetEdmTypeReference(typeof(Customer[])).AsCollection();
_writeContext = new ODataSerializerContext() { NavigationSource = _customerSet, Model = _model };
}
示例11: SetCanReflectOnInstanceType
/// <summary>
/// Sets whether reflection over the instance type is allowed or not.
/// </summary>
/// <param name="type">The type to check.</param>
/// <param name="model">Model containing annotations.</param>
/// <param name="canReflect">true if reflection over the instance type is allowed; otherwise false.</param>
public static void SetCanReflectOnInstanceType(this IEdmType type, IEdmModel model, bool canReflect)
{
ExceptionUtils.CheckArgumentNotNull(type, "type");
ExceptionUtils.CheckArgumentNotNull(model, "model");
if (type.TypeKind == EdmTypeKind.Primitive)
{
throw new ODataException(Strings.ODataQueryUtils_CannotSetMetadataAnnotationOnPrimitiveType);
}
ODataQueryEdmTypeAnnotation annotation = model.GetAnnotationValue<ODataQueryEdmTypeAnnotation>(type);
if (annotation == null)
{
if (canReflect)
{
annotation = new ODataQueryEdmTypeAnnotation { CanReflectOnInstanceType = true };
model.SetAnnotationValue(type, annotation);
}
}
else
{
annotation.CanReflectOnInstanceType = canReflect;
}
}
示例12: EnsureEpmCacheInternal
/// <summary>
/// Ensures that an up-to-date EPM cache exists for the specified <paramref name="entityType"/>.
/// If no cache exists, a new one will be created based on the public mappings (if any).
/// If the public mappings have changed (and the cache is thus dirty), the method re-constructs the cache.
/// If all public mappings have been removed, the method also removes the EPM cache.
/// </summary>
/// <param name="model">IEdmModel instance containing the annotations.</param>
/// <param name="entityType">IEdmEntityType instance for which to ensure the EPM cache.</param>
/// <param name="maxMappingCount">The maximum allowed number of entity property mappings
/// for a given entity type (on the type itself and all its base types).</param>
/// <param name="cacheModified">true if the cache was modified; otherwise false.</param>
/// <returns>An instance of <see cref="ODataEntityPropertyMappingCache"/>, if there are any EPM mappings for the given entity type, otherwise returns null.</returns>
private static ODataEntityPropertyMappingCache EnsureEpmCacheInternal(
IEdmModel model,
IEdmEntityType entityType,
int maxMappingCount,
out bool cacheModified)
{
cacheModified = false;
if (entityType == null)
{
return null;
}
// Make sure the EPM of the base type is initialized.
IEdmEntityType baseEntityType = entityType.BaseEntityType();
ODataEntityPropertyMappingCache baseCache = null;
if (baseEntityType != null)
{
baseCache = EnsureEpmCacheInternal(model, baseEntityType, maxMappingCount, out cacheModified);
}
ODataEntityPropertyMappingCache epmCache = model.GetEpmCache(entityType);
if (model.HasOwnOrInheritedEpm(entityType))
{
ODataEntityPropertyMappingCollection mappings = model.GetEntityPropertyMappings(entityType);
bool needToBuildCache = epmCache == null || cacheModified || epmCache.IsDirty(mappings);
if (needToBuildCache)
{
cacheModified = true;
int totalMappingCount = ValidationUtils.ValidateTotalEntityPropertyMappingCount(baseCache, mappings, maxMappingCount);
epmCache = new ODataEntityPropertyMappingCache(mappings, model, totalMappingCount);
// Build the EPM tree and validate it
try
{
epmCache.BuildEpmForType(entityType, entityType);
epmCache.EpmSourceTree.Validate(entityType);
epmCache.EpmTargetTree.Validate();
// We set the annotation here, so if anything fails during
// building of the cache the annotation will not even be set so
// not leaving the type in an inconsistent state.
model.SetAnnotationValue(entityType, epmCache);
}
catch
{
// Remove an existing EPM cache if it is dirty to make sure we don't leave
// stale caches in case building of the cache fails.
// NOTE: we do this in the catch block to ensure that we always make a single
// SetAnnotation call to either set or clear the existing annotation
// since the SetAnnotation method is thread-safe
model.RemoveEpmCache(entityType);
throw;
}
}
}
else
{
if (epmCache != null)
{
// remove an existing EPM cache if the mappings have been removed from the type
cacheModified = true;
model.RemoveEpmCache(entityType);
}
}
return epmCache;
}
示例13: LoadEpmAnnotations
/// <summary>
/// Loads the serializable EPM annotations on the given <paramref name="entityType"/> into their in-memory representation.
/// </summary>
/// <param name="model">The model the entity type belongs to.</param>
/// <param name="entityType">The <see cref="IEdmEntityType"/> to load the EPM annotations for.</param>
private static void LoadEpmAnnotations(IEdmModel model, IEdmEntityType entityType)
{
DebugUtils.CheckNoExternalCallers();
Debug.Assert(entityType != null, "entityType != null");
string entityTypeName = entityType.ODataFullName();
ODataEntityPropertyMappingCollection mappings = new ODataEntityPropertyMappingCollection();
// Load the annotations from the type (these are the mappings for properties not explicitly declared on this type)
model.LoadEpmAnnotations(entityType, mappings, entityTypeName, null /*property*/);
IEnumerable<IEdmProperty> declaredProperties = entityType.DeclaredProperties;
if (declaredProperties != null)
{
foreach (IEdmProperty property in declaredProperties)
{
// Load the EPM annotations for all properties independent of their kind in order to fail on invalid property kinds.
model.LoadEpmAnnotations(property, mappings, entityTypeName, property);
}
}
// Set the new EPM annotation on the entity type only at the very end to not leave a
// inconsistent annotation if building it fails.
model.SetAnnotationValue(entityType, mappings);
}
示例14: GetClrType
public static Type GetClrType(IEdmTypeReference edmTypeReference, IEdmModel edmModel, IAssembliesResolver assembliesResolver)
{
if (edmTypeReference == null)
{
throw Error.ArgumentNull("edmTypeReference");
}
Type primitiveClrType = _builtInTypesMapping
.Where(kvp => edmTypeReference.Definition.IsEquivalentTo(kvp.Value) && (!edmTypeReference.IsNullable || IsNullable(kvp.Key)))
.Select(kvp => kvp.Key)
.FirstOrDefault();
if (primitiveClrType != null)
{
return primitiveClrType;
}
else
{
ClrTypeAnnotation annotation = edmModel.GetAnnotationValue<ClrTypeAnnotation>(edmTypeReference.Definition);
if (annotation != null)
{
return annotation.ClrType;
}
IEnumerable<Type> matchingTypes = GetMatchingTypes(edmTypeReference.FullName(), assembliesResolver);
if (matchingTypes.Count() > 1)
{
throw Error.Argument("edmTypeReference", SRResources.MultipleMatchingClrTypesForEdmType,
edmTypeReference.FullName(), String.Join(",", matchingTypes.Select(type => type.AssemblyQualifiedName)));
}
edmModel.SetAnnotationValue<ClrTypeAnnotation>(edmTypeReference.Definition, new ClrTypeAnnotation(matchingTypes.SingleOrDefault()));
return matchingTypes.SingleOrDefault();
}
}
示例15: SetIsSerializedAsElement
/// <summary>
/// Sets an annotation indicating if the value should be serialized as an element.
/// </summary>
/// <param name="value">Value to set the annotation on.</param>
/// <param name="model">Model containing the value.</param>
/// <param name="isSerializedAsElement">Value indicating if the value should be serialized as an element.</param>
public static void SetIsSerializedAsElement(this IEdmValue value, IEdmModel model, bool isSerializedAsElement)
{
EdmUtil.CheckArgumentNull(value, "value");
EdmUtil.CheckArgumentNull(model, "model");
if (isSerializedAsElement)
{
EdmError error;
if (!ValidationHelper.ValidateValueCanBeWrittenAsXmlElementAnnotation(value, null, null, out error))
{
throw new InvalidOperationException(error.ToString());
}
}
model.SetAnnotationValue(value, EdmConstants.InternalUri, CsdlConstants.IsSerializedAsElementAnnotation, (object)isSerializedAsElement);
}