Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 3
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);
        }