Beispiel #1
0
        /// <summary>
        /// 只反射加载有效路径(应用程序是当前路径,Web是Bin目录)的所有程序集
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static ListX <AssemblyX> ReflectionOnlyGetAssemblies(AppDomain domain)
        {
            if (domain == null)
            {
                domain = AppDomain.CurrentDomain;
            }

            Assembly[] asms = domain.ReflectionOnlyGetAssemblies();
            if (asms == null || asms.Length < 1)
            {
                return(null);
            }

            ListX <AssemblyX> list = new ListX <AssemblyX>();

            foreach (Assembly item in asms)
            {
                list.Add(AssemblyX.Create(item));
            }

            return(list);
        }
Beispiel #2
0
        /// <summary>
        /// 只反射加载指定路径的所有程序集
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ListX <AssemblyX> ReflectionOnlyLoad(String path)
        {
            if (!Directory.Exists(path))
            {
                return(null);
            }

            String[] ss = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
            if (ss == null || ss.Length < 1)
            {
                return(null);
            }

            //AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);

            //try
            {
                ListX <AssemblyX> loadeds  = AssemblyX.GetAssemblies();
                ListX <AssemblyX> loadeds2 = AssemblyX.ReflectionOnlyGetAssemblies();

                ListX <AssemblyX> list = new ListX <AssemblyX>();
                foreach (String item in ss)
                {
                    if (!item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
                        !item.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (loadeds != null && loadeds.Exists(delegate(AssemblyX elm)
                    {
                        return(item.Equals(elm.Asm.Location, StringComparison.OrdinalIgnoreCase));
                    }))
                    {
                        continue;
                    }

                    if (loadeds2 != null && loadeds2.Exists(delegate(AssemblyX elm)
                    {
                        return(item.Equals(elm.Asm.Location, StringComparison.OrdinalIgnoreCase));
                    }))
                    {
                        continue;
                    }

                    try
                    {
                        //Assembly asm = Assembly.ReflectionOnlyLoad(File.ReadAllBytes(item));
                        Assembly asm = Assembly.ReflectionOnlyLoadFrom(item);

                        list.Add(AssemblyX.Create(asm));
                    }
                    catch { }
                }

                return(list);
            }
            //finally
            //{
            //    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
            //}
        }