Exemplo n.º 1
0
        /// <summary>
        ///     Adds a type to a <see cref="FilterCollection" /> if it doesn't already exist.
        /// </summary>
        /// <typeparam name="TType">The type of the t type.</typeparam>
        /// <param name="col">The col.</param>
        /// <param name="order">Filter order</param>
        /// <returns>FilterCollection.</returns>
        public static FilterCollection TryAdd <TType>(this FilterCollection col, int?order = null)
            where TType : IFilterMetadata
        {
            var vt = typeof(TType);

            if (col.All(t =>
            {
                // Yet more lameness, really should be able to do a simple test here...
                return(t switch
                {
                    TypeFilterAttribute tf => tf.ImplementationType != vt,
                    ServiceFilterAttribute sf => sf.ServiceType != vt,
                    FormatFilterAttribute ff => ff.GetType() != vt,
                    ResultFilterAttribute rs => rs.GetType() != vt,
                    ExceptionFilterAttribute ef => ef.GetType() != vt,
                    ActionFilterAttribute af => af.GetType() != vt,
                    _ => t.GetType() != vt
                });
            }