コード例 #1
0
        /// <summary>
        /// 从目录中找到所有的.Net程序集,并遍历所有程序集找到所有拥有指定的定制特性的类型
        /// </summary>
        /// <param name="appStartPath">The app start path.</param>
        /// <param name="targetAttribute">指定接口的类型</param>
        /// <returns></returns>
        public static Type[] FindAttributesByDirectory(string appStartPath, Type targetAttribute)
        {
            var typeList = new List <Type>();

            Assembly[] assArray = UtilAssembly.SearchAssemblyByDirectory(appStartPath);
            if (UtilCollection.IsNullOrEmpty(assArray))
            {
                return(typeList.ToArray());
            }

            Parallel.ForEach(assArray, ass =>
            {
                Type[] types = null;
                try
                {
                    types = ass.GetTypes();
                }
                catch (Exception e)
                {
                    Debug.Fail(string.Format("程序集获取Type失败。{0}", e.Message));
                }

                if (!UtilCollection.IsNullOrEmpty(types))
                {
                    typeList.AddRange(from type in types
                                      let attrs = type.GetCustomAttributes(true)
                                                  where attrs.Any(attr => attr.GetType() == targetAttribute)
                                                  select type);
                }
            });
            return(typeList.ToArray());
        }
コード例 #2
0
        /// <summary>
        /// 从目录中找到所有的.Net程序集,并遍历所有程序集找到所有指定的定制特性
        /// </summary>
        /// <param name="appStartPath">The app start path.</param>
        /// <returns></returns>
        public static T[] FindAttributes <T>(string appStartPath) where T : Attribute
        {
            var typeList = new List <T>();

            Assembly[] assArray = UtilAssembly.SearchAssemblyByDirectory(appStartPath);
            if (UtilCollection.IsNullOrEmpty(assArray))
            {
                return(typeList.ToArray());
            }

            Parallel.ForEach(assArray, ass =>
            {
                Type[] types = null;
                try
                {
                    types = ass.GetTypes();
                }
                catch (Exception e)
                {
                    Debug.Fail(string.Format("Assembly.GetTypes()异常:{0}", e.Message));
                }

                if (!UtilCollection.IsNullOrEmpty(types))
                {
                    Parallel.ForEach(types, type =>
                    {
                        object[] attrs = type.GetCustomAttributes(true);
                        typeList.AddRange(attrs.Where(attr => attr.GetType() == typeof(T)).Cast <T>());
                    });
                }
            });
            return(typeList.ToArray());
        }
コード例 #3
0
        /// <summary>
        /// 从程序集中获得元属性
        /// </summary>
        /// <param name="assemblies">程序集,如果为null,则从当前应用程序域中获取所载入的所有程序集</param>
        /// <returns>找到的元属性的数组</returns>
        public static T[] GetAttributeFromAssembly <T>(Assembly[] assemblies) where T : Attribute
        {
            var list = new List <T>();

            if (assemblies == null)
            {
                assemblies = UtilAssembly.SearchAssemblyByDirectory(AppDomain.CurrentDomain.BaseDirectory);
            }

            Parallel.ForEach(assemblies, assembly =>
            {
                var attributes = (T[])assembly.GetCustomAttributes(typeof(T), false);
                if (attributes.Length > 0)
                {
                    list.AddRange(attributes);
                }
            });
            return(list.ToArray());
        }
コード例 #4
0
        /// <summary>从指定的目录找到所有Type,并返回Type全名为Key,Type为Value的Map
        /// </summary>
        /// <param name="path">指定的目录.</param>
        /// <returns></returns>
        public static Dictionary <string, Type> FindTypeMap(string path)
        {
            if (_AppTypes.ContainsKey(path))
            {
                return(_AppTypes[path]);
            }

            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException(path + "目录不存在。");
            }

            var typeMap   = new Dictionary <string, Type>();
            var assemblys = UtilAssembly.SearchAssemblyByDirectory(path);

            foreach (var assembly in assemblys)
            {
                Type[] types;
                try
                {
                    types = assembly.GetTypes();
                }
                catch (Exception e)
                {
                    continue;
                }

                foreach (var type in types)
                {
                    if (type.FullName == null)
                    {
                        continue;
                    }
                    if (!typeMap.ContainsKey(type.FullName))
                    {
                        typeMap.Add(type.FullName, type);
                    }
                }
            }

            return(typeMap);
        }
コード例 #5
0
        /// <summary>
        /// 从目录中找到所有的.Net程序集,并遍历所有程序集找到所有指定的定制特性
        /// </summary>
        /// <param name="appStartPath">The app start path.</param>
        /// <returns></returns>
        public static List <Tuple <T, Type> > FindAttributeMap <T>(string appStartPath) where T : Attribute
        {
            var list = new List <Tuple <T, Type> >();

            Assembly[] assArray = UtilAssembly.SearchAssemblyByDirectory(appStartPath);
            if (UtilCollection.IsNullOrEmpty(assArray))
            {
                return(list);
            }

            Parallel.ForEach(assArray, ass =>
            {
                Type[] types = null;
                try
                {
                    types = ass.GetTypes();
                }
                catch (Exception e)
                {
                    Debug.Fail(string.Format("程序集获取Type失败。{0}", e.Message));
                }

                if (!UtilCollection.IsNullOrEmpty(types))
                {
                    Parallel.ForEach(types, type =>
                    {
                        object[] attrs = type.GetCustomAttributes(true);
                        if (!UtilCollection.IsNullOrEmpty(attrs))
                        {
                            list.AddRange(from attr in attrs
                                          where attr.GetType() == typeof(T)
                                          select new Tuple <T, Type>((T)attr, type));
                        }
                    });
                }
            });
            return(list);
        }