CSharpGL.AssemblyHelper.GetAssemblies C# (CSharp) Method

GetAssemblies() public static method

Get all assemblies referenced by specified asmFilename(self included) recursively.
public static GetAssemblies ( string asmFilename ) : System.Reflection.Assembly[]
asmFilename string
return System.Reflection.Assembly[]
        public static Assembly[] GetAssemblies(string asmFilename)
        {
            var result = new List<Assembly>();
            var asmPaths = new List<string>();
            var stack = new Stack<string>();
            stack.Push(asmFilename);
            while (stack.Count > 0)
            {
                string path = stack.Pop();
                Assembly asm = null;
                try
                {
                    // 检查这个路径
                    // 看是一个程序名还是一个路径
                    if ((path.IndexOf(Path.DirectorySeparatorChar, 0, path.Length) != -1) || (path.IndexOf(Path.AltDirectorySeparatorChar, 0, path.Length) != -1))
                    {
                        // 从这个路径加载程序集
                        //asm = Assembly.ReflectionOnlyLoadFrom(path);
                        asm = Assembly.LoadFrom(path);
                    }
                    else
                    {
                        // 是一个程序集名称
                        //asm = Assembly.ReflectionOnlyLoad(path);
                        asm = Assembly.Load(path);
                    }
                }
                catch (Exception)
                {
                    //TODO: add log.
                }

                // 把程序集添加到列表中
                if (asm != null)
                {
                    if (!asmPaths.Contains(path))
                    { asmPaths.Add(path); result.Add(asm); }
                    var referenced = from item in asm.GetReferencedAssemblies() select item;
                    foreach (AssemblyName item in referenced.Distinct())
                    {
                        if (!asmPaths.Contains(item.FullName))
                        {
                            stack.Push(item.FullName);
                        }
                    }
                }
            }

            return result.ToArray();
        }

Usage Example

Example #1
0
        /// <summary>
        /// Get all derived non-abstract types of specified base type from all loaded assemblies.
        /// </summary>
        /// <param name="baseType"></param>
        /// <param name="addtionalFilter">addtional filter.</param>
        /// <returns></returns>
        public static List <Type> GetAllDerivedTypes(this Type baseType, Func <Type, bool> addtionalFilter = null)
        {
            //if (addtionalFilter == null) { addtionalFilter = x => !x.IsAbstract; }
            var result = new List <Type>();

            Assembly[] assemblies = AssemblyHelper.GetAssemblies(AppDomain.CurrentDomain.BaseDirectory);
            foreach (Assembly asm in assemblies)
            {
                try
                {
                    var list = from item in asm.GetTypes()
                               where baseType.IsAssignableFrom(item) &&
                               (addtionalFilter == null || (addtionalFilter(item)))
                               orderby item.FullName
                               select item;
                    foreach (Type item in list.Distinct())
                    {
                        result.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Assembly Helper: {0}", ex));
                }
            }

            return(result);
        }
All Usage Examples Of CSharpGL.AssemblyHelper::GetAssemblies