private static object CreateFilterInstance(ConfigurationSection.MessageFilterConfigElement filter) { var filterType = Type.GetType(filter.Type); if (filterType == null) { throw new ConfigurationErrorsException(string.Format("Unable to locate filter: {0}", filter.Type)); } // Use Activator.CreateInstance here, as Message Filters should not be singletons, and need a // unique instance per message handler. return(Activator.CreateInstance(filterType)); }
/// <summary> /// Uses reflection to marshal XML attributes into public properties on the filter /// </summary> /// <param name="filter"></param> /// <param name="filterInstance"></param> private static void SetFilterProperties(ConfigurationSection.MessageFilterConfigElement filter, object filterInstance) { // Filters configured via XML can have their properties set in XML, these show up in the AdditionalAttributes collection // If there is data here, stuff it into the filter using reflection to find matching properties if (filter.AdditionalAttributes != null) { var filterProps = filterInstance.GetType().GetProperties(); foreach (var kvp in filter.AdditionalAttributes) { var prop = filterProps.FirstOrDefault(p => p.Name.ToUpper() == kvp.Key.ToUpper()); if (prop != default(PropertyInfo)) { prop.SetValue(filterInstance, Convert.ChangeType(kvp.Value, prop.PropertyType), null); } } } }