Exemple #1
0
        /// <summary>
        /// Finds the all the types that implement or inherit from the baseType. NOTE: It will only search the Rock.dll and also in assemblies that reference Rock.dll. The baseType
        /// will not be included in the result
        /// </summary>
        /// <param name="baseType">base type.</param>
        /// <param name="typeName">typeName can be specified to filter it to a specific type name</param>
        /// <returns></returns>
        public static SortedDictionary <string, Type> FindTypes(Type baseType, string typeName = null)
        {
            SortedDictionary <string, Type> types = new SortedDictionary <string, Type>();
            var assemblies = GetRockAndPluginAssemblies();

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

            return(types);
        }
Exemple #2
0
        /// <summary>
        /// Finds the all the types that implement or inherit from the baseType.  The baseType
        /// will not be included in the result
        /// </summary>
        /// <param name="baseType">base type.</param>
        /// <param name="typeName">typeName can be specified to filter it to a specific type name</param>
        /// <returns></returns>
        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);
        }
Exemple #3
0
        /// <summary>
        /// Finds the all the types that implement or inherit from the baseType.  The baseType
        /// will not be included in the result
        /// </summary>
        /// <param name="baseType">base type.</param>
        /// <param name="typeName">typeName can be specified to filter it to a specific type name</param>
        /// <returns></returns>
        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;
        }