Пример #1
0
        /// <summary>
        /// Adds an interceptor to the current ConfigSource.
        /// </summary>
        /// <param name="name">The name of the interceptor.</param>
        /// <param name="interceptorType">The type of the interceptor.</param>
        public void AddInterceptor(string name, Type interceptorType)
        {
            if (!typeof(IInterceptor).IsAssignableFrom(interceptorType))
            {
                throw new ConfigException("Type '{0}' is not an interceptor.", interceptorType);
            }

            if (this.config.Interception == null)
            {
                this.config.Interception = new InterceptionElement();
            }
            if (this.config.Interception.Interceptors == null)
            {
                this.config.Interception.Interceptors = new InterceptorElementCollection();
            }
            foreach (InterceptorElement interceptor in this.config.Interception.Interceptors)
            {
                if (interceptor.Name.Equals(name) || interceptor.Type.Equals(interceptorType.AssemblyQualifiedName))
                {
                    return;
                }
            }
            InterceptorElement interceptorAdd = new InterceptorElement();

            interceptorAdd.Name = name;
            interceptorAdd.Type = interceptorType.AssemblyQualifiedName;
            this.config.Interception.Interceptors.Add(interceptorAdd);
        }
Пример #2
0
        /// <summary>
        ///     向配置信息中添加一个拦截器
        /// </summary>
        /// <param name="name">拦截器的名字</param>
        /// <param name="interceptorType">拦截器类型</param>
        public void AddInterceptor(string name, Type interceptorType)
        {
            if (!typeof(IInterceptor).IsAssignableFrom(interceptorType))
            {
                throw new ConfigException("'{0}' 不是一个可用的拦截类型,拦截器必须继承 IInterceptor 接口", interceptorType);
            }

            if (config.Interception == null)
            {
                config.Interception = new InterceptionElement();
            }
            if (config.Interception.Interceptors == null)
            {
                config.Interception.Interceptors = new InterceptorElementCollection();
            }
            //如果已经存在相同名字或者类型的拦截器,则直接返回
            foreach (InterceptorElement interceptor in config.Interception.Interceptors)
            {
                if (interceptor.Name.Equals(name) || interceptor.Type.Equals(interceptorType.AssemblyQualifiedName))
                {
                    return;
                }
            }
            var interceptorAdd = new InterceptorElement();

            interceptorAdd.Name = name;
            interceptorAdd.Type = interceptorType.AssemblyQualifiedName;
            config.Interception.Interceptors.Add(interceptorAdd);
        }