/// <summary>
        /// Tests whether <paramref name="provider"/> has feature <typeparamref name="TFeature"/>.
        /// </summary>
        /// <typeparam name="TFeature">A type of required feature.</typeparam>
        /// <param name="condition">An ensure condition helper.</param>
        /// <param name="provider">A provider to test feature on.</param>
        /// <exception cref="ArgumentOutOfRangeException">When <paramref name="provider"/> doesn't have feature of type <typeparamref name="TFeature"/>.</exception>
        public static void HasFeature <TFeature>(this EnsureConditionHelper condition, IFeatureProvider provider)
        {
            Ensure.NotNull(condition, "condition");
            Ensure.NotNull(provider, "model");

            TFeature feature;

            if (!provider.TryWith(out feature))
            {
                throw Ensure.Exception.ArgumentOutOfRange("model", "Feature of type '{0}' is required on '{1}'.", typeof(TFeature).FullName, provider);
            }
        }
Пример #2
0
        /// <summary>
        /// Tries to retrieve object of type <typeparamref name="TFeature"/>.
        /// If this is not possible, throws <see cref="NotSupportedException"/>/
        /// </summary>
        /// <typeparam name="TFeature">A type of the feature to retrieve.</typeparam>
        /// <param name="provider">A feature provider.</param>
        /// <returns>Feature of type <typeparamref name="TFeature"/>.</returns>
        /// <exception cref="NotSupportedException">If <paramref name="provider"/> doesn't support feature of type <typeparamref name="TFeature"/>.</exception>
        public static TFeature With <TFeature>(this IFeatureProvider provider)
        {
            Ensure.NotNull(provider, "model");

            TFeature feature;

            if (provider.TryWith(out feature))
            {
                return(feature);
            }

            throw Ensure.Exception.NotSupported(
                      "Feature provider '{0}' doesn't support feature '{1}'.",
                      provider.GetType().FullName,
                      typeof(TFeature).FullName
                      );
        }