/// <summary> /// Creates TypeFilter with type information from the string array of type names. /// </summary> /// <param name="rawFilter">String array containing names of types to be included in the filter.</param> /// <returns>TypeFilter object containing type information.</returns> private TypeFilter LoadTypeFilter(string[] rawFilter) { // Initialize filter TypeFilter typeFilter = new TypeFilter(); // Verify information was passed in if (rawFilter != null) { TypeInfo exceptionTypeInfo; // Loop through the string array for (int i = 0; i < rawFilter.GetLength(0); i++) { // If the wildcard character "*" exists set the TypeFilter to accept all types. if (rawFilter[i] == "*") { typeFilter.AcceptAllTypes = true; } else { try { if (rawFilter[i].Length > 0) { // Create the TypeInfo class exceptionTypeInfo = new TypeInfo(); // If the string starts with a "+" if (rawFilter[i].Trim().StartsWith("+")) { // Set the TypeInfo class to include subclasses exceptionTypeInfo.IncludeSubClasses = true; // Get the Type class from the filter privided. exceptionTypeInfo.ClassType = Type.GetType(rawFilter[i].Trim().TrimStart(Convert.ToChar("+")), true); } else { // Set the TypeInfo class not to include subclasses exceptionTypeInfo.IncludeSubClasses = false; // Get the Type class from the filter privided. exceptionTypeInfo.ClassType = Type.GetType(rawFilter[i].Trim(), true); } // Add the TypeInfo class to the TypeFilter typeFilter.Types.Add(exceptionTypeInfo); } } catch (TypeLoadException e) { // If the Type could not be created throw a configuration exception. ExceptionManager.PublishInternalException(new ConfigurationErrorsException(resourceManager.GetString("RES_EXCEPTION_LOADING_CONFIGURATION"), e), null); } } } } return typeFilter; }
/// <summary> /// Determines if a type is contained the supplied filter. /// </summary> /// <param name="type">The Type to look for</param> /// <param name="typeFilter">The Filter to test against the Type</param> /// <returns>true or false</returns> private bool MatchesFilter(Type type, TypeFilter typeFilter) { TypeInfo typeInfo; // If no filter is provided type does not match the filter. if (typeFilter == null) return false; // If all types are accepted in the filter (using the "*") return true. if (typeFilter.AcceptAllTypes) return true; // Loop through the types specified in the filter. for (int i = 0; i < typeFilter.Types.Count; i++) { typeInfo = (TypeInfo)typeFilter.Types[i]; // If the Type matches this type in the Filter, then return true. if (typeInfo.ClassType.Equals(type)) return true; // If the filter type includes SubClasses of itself (it had a "+" before the type in the // configuration file) AND the Type is a SubClass of the filter type, then return true. if (typeInfo.IncludeSubClasses == true && typeInfo.ClassType.IsAssignableFrom(type)) return true; } // If no matches are found return false. return false; }