public void Init()
        {
            this._address = new Address {
                Street = "123 fake st"
            };
            this._entity = new Customer {
                Id = 1, Address = this._address, Emails = new List <string> {
                    "*****@*****.**"
                }
            };

            var model       = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityType  = model.GetOrCreateEdmType(typeof(Customer));
            var complexType = model.GetOrCreateEdmType(typeof(Address));

            this._complexValue = new ClientEdmStructuredValue(this._address, model, model.GetClientTypeAnnotation(complexType));
            this._entityValue  = new ClientEdmStructuredValue(this._entity, model, model.GetClientTypeAnnotation(entityType));
        }
Пример #2
0
        /// <summary>
        /// Evaluate IEdmVocabularyAnnotation to an CLR object
        /// </summary>
        /// <typeparam name="TResult">The CLR type of the annotation to be returned.</typeparam>
        /// <param name="context">The data service context.</param>
        /// <param name="edmValueAnnotation">IEdmVocabularyAnnotation to be evaluated.</param>
        /// <param name="clientEdmValue">Value to use as context in evaluating the expression.</param>
        /// <param name="annotationValue">Value of the term evaluated.</param>
        /// <returns>True if the annotation value can be evaluated, else false.</returns>
        private static bool TryEvaluateMetadataAnnotation <TResult>(DataServiceContext context, IEdmVocabularyAnnotation edmValueAnnotation, ClientEdmStructuredValue clientEdmValue, out TResult annotationValue)
        {
            if (edmValueAnnotation == null)
            {
                annotationValue = default(TResult);
                return(false);
            }

            EdmToClrEvaluator evaluator = CreateEdmToClrEvaluator(context);

            try
            {
                annotationValue = evaluator.EvaluateToClrValue <TResult>(edmValueAnnotation.Value, clientEdmValue);
            }
            catch (InvalidOperationException)
            {
                // When expression contains Path. if the clientEdmValue is null, or the related property is not valid property of the clientEdmValue.
                // TheEvaluateToClrValue might throw InvalidOperationException;
                annotationValue = default(TResult);
                return(false);
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Gets the CLR value of a term that has been applied to the specified object
        /// </summary>
        /// <typeparam name="TResult">The CLR type of the annotation to be returned.</typeparam>
        /// <param name="context">The data service context.</param>
        /// <param name="source">The specified annotated object instance.</param>
        /// <param name="term">The term name.</param>
        /// <param name="qualifier">The qualifier name.</param>
        /// <param name="annotationValue">Value of the term evaluated.</param>
        /// <returns>True if the annotation value can be evaluated, else false.</returns>
        internal static bool TryGetMetadataAnnotation <TResult>(DataServiceContext context, object source, string term, string qualifier, out TResult annotationValue)
        {
            IEdmVocabularyAnnotation edmValueAnnotation = null;
            ClientEdmStructuredValue clientEdmValue     = null;
            PropertyInfo             propertyInfo       = null;
            MethodInfo methodInfo = null;

            var keyValue = source as Tuple <object, MemberInfo>;

            if (keyValue != null)
            {
                // Get metadata annotation defined on property or Navigation property or Operation or OperationImport
                var instance   = keyValue.Item1;
                var memberInfo = keyValue.Item2;
                propertyInfo = memberInfo as PropertyInfo;
                methodInfo   = memberInfo as MethodInfo;
                if (instance != null)
                {
                    IEdmType edmType = context.Model.GetOrCreateEdmType(instance.GetType());
                    if (edmType is IEdmStructuredType)
                    {
                        ClientTypeAnnotation clientTypeAnnotation = context.Model.GetClientTypeAnnotation(edmType);
                        clientEdmValue = new ClientEdmStructuredValue(instance, context.Model, clientTypeAnnotation);
                    }
                }
            }
            else
            {
                if (propertyInfo == null)
                {
                    propertyInfo = source as PropertyInfo;
                }

                if (methodInfo == null)
                {
                    methodInfo = source as MethodInfo;
                }
            }

            if (propertyInfo != null)
            {
                edmValueAnnotation = GetOrInsertCachedMetadataAnnotationForPropertyInfo(context, propertyInfo, term, qualifier);
                return(TryEvaluateMetadataAnnotation(context, edmValueAnnotation, clientEdmValue, out annotationValue));
            }

            if (methodInfo != null)
            {
                edmValueAnnotation = GetOrInsertCachedMetadataAnnotationForMethodInfo(context, methodInfo, term, qualifier);
                return(TryEvaluateMetadataAnnotation(context, edmValueAnnotation, clientEdmValue, out annotationValue));
            }

            var  type           = source as Type;
            Type underlyingType = type;

            if (type == null)
            {
                type           = source.GetType();
                underlyingType = Nullable.GetUnderlyingType(type) ?? type;

                // For complex type or entity type instance, try to convert the instance to ClientEdmStructuredValue for further evaluation.
                IEdmType edmType = context.Model.GetOrCreateEdmType(underlyingType);
                if (edmType is IEdmStructuredType)
                {
                    ClientTypeAnnotation clientTypeAnnotation = context.Model.GetClientTypeAnnotation(edmType);
                    clientEdmValue = new ClientEdmStructuredValue(source, context.Model, clientTypeAnnotation);
                }
            }

            edmValueAnnotation = GetOrInsertCachedMetadataAnnotationForType(context, underlyingType, term, qualifier);
            return(TryEvaluateMetadataAnnotation(context, edmValueAnnotation, clientEdmValue, out annotationValue));
        }