Example #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);
        }
Example #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);
            //}
        }
Example #3
0
        /// <summary>
        /// 根据名称获取类型
        /// </summary>
        /// <param name="typeName">类型名</param>
        /// <param name="isLoadAssembly">是否从未加载程序集中获取类型。使用仅反射的方法检查目标类型,如果存在,则进行常规加载</param>
        /// <returns></returns>
        public static Type GetType(String typeName, Boolean isLoadAssembly)
        {
            if (String.IsNullOrEmpty(typeName))
            {
                throw new ArgumentNullException("typeName");
            }

            // 基本获取
            Type type = Type.GetType(typeName);

            if (type != null)
            {
                return(type);
            }

            // 尝试本程序集
            Assembly[] asms = new[] {
                Assembly.GetExecutingAssembly(),
                       Assembly.GetCallingAssembly(),
                       Assembly.GetEntryAssembly()
            };

            foreach (Assembly asm in asms)
            {
                if (asm != null)
                {
                    type = asm.GetType(typeName);

                    if (type != null)
                    {
                        return(type);
                    }
                }
            }

            // 尝试所有程序集
            ListX <AssemblyX> list = AssemblyX.GetAssemblies();

            if (list != null && list.Count > 0)
            {
                foreach (AssemblyX asm in list)
                {
                    type = asm.Asm.GetType(typeName);
                    if (type != null)
                    {
                        return(type);
                    }
                }
            }

            // 尝试加载程序集
            AssemblyX.ReflectionOnlyLoad();
            list = AssemblyX.ReflectionOnlyGetAssemblies();
            if (list != null && list.Count > 0)
            {
                foreach (AssemblyX asm in list)
                {
                    type = asm.Asm.GetType(typeName);
                    if (type != null)
                    {
                        // 真实加载
                        Assembly asm2  = Assembly.LoadFile(asm.Asm.Location);
                        Type     type2 = asm2.GetType(typeName);
                        if (type2 != null)
                        {
                            type = type2;
                        }

                        return(type);
                    }
                }
            }

            return(null);
        }