Rock.Reflection.FindTypes C# (CSharp) Method

FindTypes() public static method

Finds the all the types that implement or inherit from the baseType. The baseType will not be included in the result
public static FindTypes ( Type baseType, string typeName = null ) : Type>.SortedDictionary
baseType System.Type base type.
typeName string typeName can be specified to filter it to a specific type name
return Type>.SortedDictionary
        public static SortedDictionary<string, Type> FindTypes( Type baseType, string typeName = null )
        {
            SortedDictionary<string, Type> types = new SortedDictionary<string, Type>();

            Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();

            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            assemblies.Add( executingAssembly.FullName.ToLower(), executingAssembly );

            foreach ( Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() )
            {
                if ( assembly.GlobalAssemblyCache || assembly.IsDynamic )
                {
                    continue;
                }

                bool searchAssembly = false;

                string fileName = Path.GetFileName( assembly.CodeBase );

                // only search inside dlls that are Rock.dll or reference Rock.dll
                if ( fileName.Equals( "Rock.dll", StringComparison.OrdinalIgnoreCase ) )
                {
                    searchAssembly = true;
                }
                else
                {
                    List<AssemblyName> referencedAssemblies = assembly.GetReferencedAssemblies().ToList();

                    if ( referencedAssemblies.Any( a => a.Name.Equals( "Rock", StringComparison.OrdinalIgnoreCase ) ) )
                    {
                        searchAssembly = true;
                    }
                }

                if ( searchAssembly )
                {
                    if ( !assemblies.Keys.Contains( assembly.FullName.ToLower() ) )
                    {
                        assemblies.Add( assembly.FullName.ToLower(), assembly );
                    }
                }
            }

            // Add any dll's in the Plugins folder
            var httpContext = System.Web.HttpContext.Current;
            if ( httpContext != null )
            {
                var pluginsDir = new DirectoryInfo( httpContext.Server.MapPath( "~/Plugins" ) );
                if ( pluginsDir.Exists )
                {
                    foreach ( var file in pluginsDir.GetFiles( "*.dll", SearchOption.AllDirectories ) )
                    {
                        var assembly = Assembly.LoadFrom( file.FullName );
                        if ( !assemblies.Keys.Contains( assembly.FullName.ToLower() ) )
                        {
                            assemblies.Add( assembly.FullName.ToLower(), assembly );
                        }
                    }
                }
            }

            foreach ( KeyValuePair<string, Assembly> assemblyEntry in assemblies )
            {
                var typeEntries = SearchAssembly( assemblyEntry.Value, baseType );
                foreach ( KeyValuePair<string, Type> typeEntry in typeEntries )
                {
                    if ( string.IsNullOrWhiteSpace( typeName ) || typeEntry.Key == typeName )
                    {
                        types.AddOrIgnore( typeEntry.Key, typeEntry.Value );
                    }
                }
            }

            return types;
        }