Exemple #1
0
        /// <summary>
        /// get property wrapper for a property name, might be method around open types for otherwise unknown properties
        /// </summary>
        /// <param name="propertyName">property name</param>
        /// <param name="undeclaredPropertyBehavior">UndeclaredPropertyBehavior</param>
        /// <returns>property wrapper</returns>
        /// <exception cref="InvalidOperationException">for unknown properties on closed types</exception>
        internal ClientPropertyAnnotation GetProperty(string propertyName, UndeclaredPropertyBehavior undeclaredPropertyBehavior)
        {
            Debug.Assert(propertyName != null, "property name");
            if (this.clientPropertyCache == null)
            {
                this.BuildPropertyCache();
            }

            ClientPropertyAnnotation property;

            if (!this.clientPropertyCache.TryGetValue(propertyName, out property))
            {
                string propertyClientName = ClientTypeUtil.GetClientPropertyName(this.ElementType, propertyName, undeclaredPropertyBehavior);
                if ((string.IsNullOrEmpty(propertyClientName) || !this.clientPropertyCache.TryGetValue(propertyClientName, out property)) && (undeclaredPropertyBehavior == UndeclaredPropertyBehavior.ThrowException))
                {
#if OPENSILVER
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.ClientType_MissingProperty(this.ElementTypeName, propertyName));
#else
                    throw Microsoft.OData.Client.Error.InvalidOperation(Microsoft.OData.Client.Strings.ClientType_MissingProperty(this.ElementTypeName, propertyName));
#endif
                }
            }

            return property;
        }
Exemple #2
0
        /// <summary>
        /// Set instance annotation for a property
        /// </summary>
        /// <param name="propertyName">Property name</param>
        /// <param name="instanceAnnotations">Intance annotations to be set</param>
        /// <param name="type">The type of the containing object</param>
        /// <param name="declaringInstance">The containing object instance</param>
        private void SetInstanceAnnotations(string propertyName, IDictionary <string, object> instanceAnnotations, Type type, object declaringInstance)
        {
            if (declaringInstance != null)
            {
                bool ignoreMissingProperty = this.MaterializerContext.Context.IgnoreMissingProperties;

                // Get the client property info
                string clientPropertyName = ClientTypeUtil.GetClientPropertyName(type, propertyName, ignoreMissingProperty);
                if (clientPropertyName != null)
                {
                    PropertyInfo propertyInfo  = type.GetProperty(clientPropertyName);
                    Type         declaringType = propertyInfo.DeclaringType;

                    if (type.IsSubclassOf(declaringType))
                    {
                        propertyInfo = declaringType.GetProperty(clientPropertyName);
                    }

                    Tuple <object, MemberInfo> annotationKeyForProperty = new Tuple <object, MemberInfo>(declaringInstance, propertyInfo);
                    SetInstanceAnnotations(annotationKeyForProperty, instanceAnnotations);
                }
            }
        }