Exemplo n.º 1
0
        /// <summary>
        /// Converts the inner object of a <see cref="TaskHost"/> to the specified type so that
        /// actions can be performed on it. This extends <see cref="Executable"/> but it must
        /// be castable to a <see cref="TaskHost"/> to function.
        /// </summary>
        /// <typeparam name="T">The type of the inner object.</typeparam>
        /// <param name="executable">The Executable to evaluate.</param>
        /// <returns>The converted inner object.</returns>
        /// <exception cref="InvalidCastException">Thrown if the executable is not a TaskHost, or if the inner object can't be cast to the specified type.</exception>
        public static T ConvertTo <T>(this Executable executable)
            where T : class
        {
            if (executable == null)
            {
                throw new ArgumentNullException(nameof(executable));
            }

            TaskHost taskHost = executable as TaskHost;

            if (taskHost == null)
            {
                throw new InvalidCastException();
            }

            return(taskHost.ConvertTo <T>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the inner object of a <see cref="TaskHost"/> to the specified type so that
        /// actions can be performed on it.
        /// </summary>
        /// <typeparam name="T">The type of the inner object.</typeparam>
        /// <param name="taskHost">The TaskHost to evaluate.</param>
        /// <param name="action">The action to take on the inner object.</param>
        /// <returns>The <see cref="taskHost"/> that was passed in.</returns>
        /// <exception cref="InvalidCastException">Thrown if the inner object can't be cast to the specified type.</exception>
        public static TaskHost As <T>(this TaskHost taskHost, Action <T> action)
            where T : class
        {
            if (taskHost == null)
            {
                throw new ArgumentNullException(nameof(taskHost));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var innerObject = taskHost.ConvertTo <T>();

            action(innerObject);

            return(taskHost);
        }