/// <summary>
        /// Tries to get property value.
        /// </summary>
        /// <param name="propertyDefinition">The property definition.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <typeparam name="T">Type of expected property value.</typeparam>
        /// <returns>True if property exists in collection.</returns>
        internal bool TryGetValue <T>(ExtendedPropertyDefinition propertyDefinition, out T propertyValue)
        {
            ExtendedProperty extendedProperty;

            if (this.TryGetProperty(propertyDefinition, out extendedProperty))
            {
                // Verify that the type parameter and property definition's type are compatible.
                if (!typeof(T).IsAssignableFrom(propertyDefinition.Type))
                {
                    string errorMessage = string.Format(
                        Strings.PropertyDefinitionTypeMismatch,
                        EwsUtilities.GetPrintableTypeName(propertyDefinition.Type),
                        EwsUtilities.GetPrintableTypeName(typeof(T)));
                    throw new ArgumentException(errorMessage, "propertyDefinition");
                }

                propertyValue = (T)extendedProperty.Value;
                return(true);
            }
            else
            {
                propertyValue = default(T);
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Tries to get a property value based on a property definition.
        /// </summary>
        /// <typeparam name="T">The types of the property.</typeparam>
        /// <param name="propertyDefinition">The property definition.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <returns>True if property was retrieved.</returns>
        internal bool TryGetProperty <T>(PropertyDefinition propertyDefinition, out T propertyValue)
        {
            // Verify that the type parameter and property definition's type are compatible.
            if (!typeof(T).IsAssignableFrom(propertyDefinition.Type))
            {
                string errorMessage = string.Format(
                    Strings.PropertyDefinitionTypeMismatch,
                    EwsUtilities.GetPrintableTypeName(propertyDefinition.Type),
                    EwsUtilities.GetPrintableTypeName(typeof(T)));
                throw new ArgumentException(errorMessage, "propertyDefinition");
            }

            object value;

            bool result = this.TryGetProperty(propertyDefinition, out value);

            propertyValue = result ? (T)value : default(T);

            return(result);
        }