Exemplo n.º 1
0
        /// <summary>
        /// Осуществляет проверку на равенство с указанным объектом.
        /// </summary>
        /// <param name="obj">Объект, с которым требуется сравнить.</param>
        /// <returns>Флаг: <c>true</c>, если объекты равны, и <c>false</c>, в противном случае.</returns>
        public override bool Equals(object obj)
        {
            DataObjectDictionary anotherDataObject = obj as DataObjectDictionary;

            if (anotherDataObject == null)
            {
                return(false);
            }

            if (Count != anotherDataObject.Count)
            {
                return(false);
            }

            foreach (KeyValuePair <string, object> anotherProperty in anotherDataObject)
            {
                string anotherPropertyName  = anotherProperty.Key;
                object anotherPropertyValue = anotherProperty.Value;
                Type   anotherPropertyType;

                object propertyValue;
                Type   propertyType;
                if (!TryGetValue(anotherProperty.Key, out propertyValue))
                {
                    return(false);
                }

                if (anotherPropertyValue == null)
                {
                    if (propertyValue != null)
                    {
                        return(false);
                    }
                    else
                    {
                        continue;
                    }
                }

                anotherPropertyType = anotherPropertyValue.GetType();
                propertyType        = propertyValue.GetType();
                if (anotherPropertyType != propertyType)
                {
                    return(false);
                }

                MethodInfo anotherPropertyEqualsMethod = anotherPropertyType.GetMethod("Equals", new[] { typeof(object) });
                if (!(bool)anotherPropertyEqualsMethod.Invoke(anotherPropertyValue, new[] { propertyValue }))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Осуществляет получение значений для указанного свойства.
        /// </summary>
        /// <remarks>Свойства можно указывать через точку: Медведь.ЛесОбитания.Страна.Название.</remarks>
        /// <param name="propertyPath">Путь к свойству.</param>
        /// <returns>Значение указанного свойства</returns>
        public object GetPropertyValue(string propertyPath)
        {
            object propertyValue            = null;
            DataObjectDictionary dictionary = this;

            string[] properties = propertyPath.Split('.');
            foreach (string property in properties)
            {
                if (dictionary == null || !dictionary.ContainsKey(property))
                {
                    throw new Exception("Property \"{0}\" does not exist in data object dictionary.");
                }

                // Значение свойства.
                propertyValue = dictionary[property];

                // Словарь мастерового объекта;
                dictionary = propertyValue as DataObjectDictionary;
            }

            return(propertyValue);
        }