Ejemplo n.º 1
0
        /// <summary>
        /// Registers a custom filter that is decorated with CustomFilterAttribute
        /// </summary>
        /// <param name="t">Type of the filter to register</param>
        public void RegisterCustomFilter(Type t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }
            object[] customAttrs = t.GetCustomAttributes(typeof(CustomFilterAttribute), false);

            if (customAttrs.Length < 1)
            {
                throw new ArgumentException("Given type is not decorated with CustomFilterAttribute");
            }
            CustomFilterAttribute attr = customAttrs[0] as CustomFilterAttribute;

            if (attr != null)
            {
                string typeName = t.AssemblyQualifiedName;
                if (!typeof(IFilter).IsAssignableFrom(t))
                {
                    LogbusException ex = new LogbusException("Given type does not implement IFilter");
                    ex.Data.Add("typeName", t);
                    throw ex;
                }


                if (_registeredTypes.ContainsKey(attr.Tag))
                {
                    _registeredTypes.Remove(attr.Tag);
                }

                _registeredTypes.Add(attr.Tag, typeName);
                _registeredDescriptions.Add(attr.Tag, attr.Description);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Scans an assembly for user-defined filters and registers all of them
        /// </summary>
        /// <param name="assemblyToScan">Assembly that must be scanned for filters</param>
        public void ScanAssemblyAndRegister(Assembly assemblyToScan)
        {
            if (assemblyToScan == null)
            {
                throw new ArgumentNullException("assemblyToScan");
            }
            foreach (Type t in assemblyToScan.GetTypes())
            {
                string   typename    = t.AssemblyQualifiedName;
                object[] customAttrs = t.GetCustomAttributes(typeof(CustomFilterAttribute), false);
                if (customAttrs.Length < 1)
                {
                    continue;
                }
                CustomFilterAttribute attr = customAttrs[0] as CustomFilterAttribute;


                if (attr != null)
                {
                    RegisterCustomFilter(attr.Tag, typename, attr.Description);
                }
            }
        }