/// <summary>
        /// Gets the desired property value as the desired type. Throws an exception if the property is not supported or is not cached during a cached request.
        /// </summary>
        /// <typeparam name="T">The type of the value to get.</typeparam>
        /// <param name="property">The <see cref="PropertyId"/> of the property to get the value from.</param>
        /// <returns>The value of the property.</returns>
        public T GetPropertyValue <T>(PropertyId property)
        {
            if (Equals(property, PropertyId.NotSupportedByFramework))
            {
                throw new NotSupportedByFrameworkException();
            }
            var isCacheActive = CacheRequest.IsCachingActive;

            try
            {
                var value = InternalGetPropertyValue(property.Id, isCacheActive, false);
                if (value == Automation.NotSupportedValue)
                {
                    throw new PropertyNotSupportedException(property);
                }
                return(property.Convert <T>(Automation, value));
            }
            catch (Exception ex)
            {
                if (isCacheActive)
                {
                    var cacheRequest = CacheRequest.Current;
                    if (!cacheRequest.Properties.Contains(property))
                    {
                        throw new PropertyNotCachedException(property, ex);
                    }
                }
                // Should actually never come here
                throw;
            }
        }
Пример #2
0
 /// <summary>
 /// Tries to get the property value as the desired type. Throws an exception if the property is not cached during a cached request.
 /// </summary>
 /// <typeparam name="T">The type of the value to get.</typeparam>
 /// <param name="property">The <see cref="PropertyId"/> of the property to get the value from.</param>
 /// <param name="value">The out object where the value should be put. Is the default if the property is not supported.</param>
 /// <returns>True if the property is supported and false otherwise.</returns>
 public bool TryGetPropertyValue <T>(PropertyId property, out T value)
 {
     if (Equals(property, PropertyId.NotSupportedByFramework))
     {
         throw new NotSupportedByFrameworkException();
     }
     try
     {
         var internalValue = InternalGetPropertyValue(property.Id, false);
         if (internalValue == Automation.NotSupportedValue)
         {
             value = default;
             return(false);
         }
         value = property.Convert <T>(Automation, internalValue);
         return(true);
     }
     catch (Exception ex)
     {
         if (!(ex is COMException))
         {
             throw;
         }
         value = default;
         return(false);
     }
 }
Пример #3
0
        public T GetPropertyValue <T>(PropertyId property, bool cached)
        {
            var value = InternalGetPropertyValue(property.Id, cached, false);

            if (value == Automation.NotSupportedValue)
            {
                throw new PropertyNotSupportedException(String.Format("Property '{0}' not supported", property.Name), property);
            }
            return(property.Convert <T>(value));
        }
Пример #4
0
        public bool TryGetPropertyValue <T>(PropertyId property, bool cached, out T value)
        {
            var tmp = InternalGetPropertyValue(property.Id, cached, false);

            if (tmp == Automation.NotSupportedValue)
            {
                value = default(T);
                return(false);
            }
            value = property.Convert <T>(tmp);
            return(true);
        }
Пример #5
0
        /// <summary>
        /// Gets the desired property value as the desired type. Throws an exception if the property is not supported or is not cached during a cached request.
        /// </summary>
        /// <typeparam name="T">The type of the value to get.</typeparam>
        /// <param name="property">The <see cref="PropertyId"/> of the property to get the value from.</param>
        /// <returns>The value of the property.</returns>
        public T GetPropertyValue <T>(PropertyId property)
        {
            if (Equals(property, PropertyId.NotSupportedByFramework))
            {
                throw new NotSupportedByFrameworkException();
            }

            var value = InternalGetPropertyValue(property.Id, false);

            if (value == Automation.NotSupportedValue)
            {
                throw new PropertyNotSupportedException(property);
            }
            return(property.Convert <T>(Automation, value));
        }
Пример #6
0
        public T SafeGetPropertyValue <T>(PropertyId property, bool cached)
        {
            var value = InternalGetPropertyValue(property.Id, cached, true);

            return(property.Convert <T>(value));
        }