GetPropertyData() публичный Метод

Gets the property data.
The is null. The is null or whitespace. Thrown when the property is not registered.
public GetPropertyData ( Type type, string name ) : PropertyData
type System.Type The type for which to get the property data.
name string The name of the property.
Результат PropertyData
Пример #1
0
        /// <summary>
        /// Reads the value from the XML node.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="propertyName"/> is <c>null</c> or whitespace.</exception>
        /// <remarks>This method does not check whether the property exists. This is the responsibility of the caller.</remarks>
        private void ReadValueFromXmlNode(XmlReader reader, string propertyName)
        {
            Argument.IsNotNull("reader", reader);
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);

            var propertyData = PropertyDataManager.GetPropertyData(GetType(), propertyName);

            object value = null;

            switch (reader.NodeType)
            {
            case XmlNodeType.Attribute:
                value = GetObjectFromXmlAttribute(reader, propertyData);
                break;

            case XmlNodeType.Element:
                value = GetObjectFromXmlElement(reader, propertyName);
                break;

            default:
                string error = string.Format("Xml node type '{0}' with local name '{1}' is not supported", reader.NodeType, ObjectToStringHelper.ToString(reader.LocalName));
                Log.Error(error);
                throw new NotSupportedException(error);
            }

            if (value != null)
            {
                SetValue(propertyData, value, false, false);
            }
        }
        /// <summary>
        /// Gets the typed value of a specific property.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="name">Name of the property.</param>
        /// <returns>Object value of the property.</returns>
        /// <exception cref="PropertyNotRegisteredException">The property is not registered.</exception>
        protected TValue GetValue <TValue>(string name)
        {
            Argument.IsNotNullOrEmpty("name", name);

            var propertyData = PropertyDataManager.GetPropertyData(GetType(), name);

            return(GetValue <TValue>(propertyData));
        }
        /// <summary>
        /// Gets the value of a specific property.
        /// </summary>
        /// <param name="name">Name of the property.</param>
        /// <returns>Object value of the property.</returns>
        /// <exception cref="PropertyNotRegisteredException">The property is not registered.</exception>
        protected internal object GetValue(string name)
        {
            Argument.IsNotNullOrEmpty("name", name);

            var propertyData = PropertyDataManager.GetPropertyData(GetType(), name);

            return(GetValue(propertyData));
        }
Пример #4
0
        /// <summary>
        /// When overridden in a derived class, determines whether two objects of type <see cref="ModelBase" /> are equal.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>true if the specified objects are equal; otherwise, false.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Equals(ModelBase x, ModelBase y)
        {
            if (ReferenceEquals(x, y))
            {
                return(true);
            }

            if (((object)x == null) || ((object)y == null))
            {
                return(false);
            }

            // Check types before the "expensive" operation of checking all property values
            var xType = x.GetType();
            var yType = y.GetType();

            if (xType != yType)
            {
                return(false);
            }

            if (!CompareProperties)
            {
                return(false);
            }

            lock (x._lock)
            {
                foreach (var propertyValue in x._propertyBag.GetAllProperties())
                {
                    var propertyData = PropertyDataManager.GetPropertyData(xType, propertyValue.Key);

                    // Only check if this is not an internal data object base property
                    if (!propertyData.IsModelBaseProperty)
                    {
                        object valueA = propertyValue.Value;
                        object valueB = y.GetValue(propertyValue.Key);

                        if (!ReferenceEquals(valueA, valueB))
                        {
                            if ((valueA == null) || (valueB == null))
                            {
                                return(false);
                            }

                            // Is this an IEnumerable (but not a string)?
                            var valueAAsIEnumerable = valueA as IEnumerable;
                            if ((valueAAsIEnumerable != null) && !(valueA is string))
                            {
                                // Yes, loop all sub items and check them
                                if (CompareCollections)
                                {
                                    if (!CollectionHelper.IsEqualTo(valueAAsIEnumerable, (IEnumerable)valueB))
                                    {
                                        return(false);
                                    }
                                }
                            }
                            else
                            {
                                // No, check objects via equals method
                                if (CompareValues)
                                {
                                    if (!valueA.Equals(valueB))
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
Пример #5
0
 /// <summary>
 /// Gets the <see cref="PropertyData"/> for the specified property.
 /// </summary>
 /// <param name="name">The name of the property.</param>
 /// <returns>The <see cref="PropertyData"/>.</returns>
 /// <exception cref="PropertyNotRegisteredException">The property is not registered.</exception>
 protected PropertyData GetPropertyData(string name)
 {
     return(PropertyDataManager.GetPropertyData(GetType(), name));
 }
Пример #6
0
        /// <summary>
        /// Validates the property using data annotations.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns><c>true</c> if no errors using data annotations are found; otherwise <c>false</c>.</returns>
        private bool ValidatePropertyUsingAnnotations(string propertyName)
        {
            if (!ValidateUsingDataAnnotations)
            {
                return(true);
            }

            var validationSuspensionContext = _validationSuspensionContext;

            if (validationSuspensionContext != null)
            {
                validationSuspensionContext.Add(propertyName);
                return(true);
            }

#if !NETFX_CORE && !PCL
            var type = GetType();

            try
            {
                if (!PropertiesNotCausingValidation[type].Contains(propertyName))
                {
                    object value   = null;
                    var    handled = false;

                    var propertyDataManager = PropertyDataManager;
                    if (propertyDataManager.IsPropertyRegistered(type, propertyName))
                    {
                        var catelPropertyData = PropertyDataManager.GetPropertyData(type, propertyName);
                        if (catelPropertyData != null)
                        {
                            var propertyInfo = catelPropertyData.GetPropertyInfo(type);
                            if (propertyInfo == null || !propertyInfo.HasPublicGetter)
                            {
                                PropertiesNotCausingValidation[type].Add(propertyName);
                                return(false);
                            }

                            value   = GetValue(catelPropertyData);
                            handled = true;
                        }
                    }

                    if (!handled)
                    {
                        if (!PropertyHelper.IsPublicProperty(this, propertyName))
                        {
                            Log.Debug("Property '{0}' is not a public property, cannot validate non-public properties in the current platform", propertyName);

                            PropertiesNotCausingValidation[type].Add(propertyName);
                            return(false);
                        }

                        value = PropertyHelper.GetPropertyValue(this, propertyName);
                    }

                    if (!_dataAnnotationsValidationContext.ContainsKey(propertyName))
                    {
                        _dataAnnotationsValidationContext[propertyName] = new System.ComponentModel.DataAnnotations.ValidationContext(this, null, null)
                        {
                            MemberName = propertyName
                        };
                    }

                    System.ComponentModel.DataAnnotations.Validator.ValidateProperty(value, _dataAnnotationsValidationContext[propertyName]);

                    // If succeeded, clear any previous error
                    if (_dataAnnotationValidationResults.ContainsKey(propertyName))
                    {
                        _dataAnnotationValidationResults[propertyName] = null;
                    }
                }
            }
            catch (System.ComponentModel.DataAnnotations.ValidationException validationException)
            {
                _dataAnnotationValidationResults[propertyName] = validationException.Message;
                return(false);
            }
            catch (Exception ex)
            {
                PropertiesNotCausingValidation[type].Add(propertyName);

                Log.Warning(ex, "Failed to validate property '{0}' via Validator (property does not exists?)", propertyName);
            }
#endif

            return(true);
        }