Пример #1
0
        /// <summary>
        /// 查找指定程序集中所有的服务类
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static PluginInfo[] GetPluginsInAssembly(Assembly assembly)
        {
            System.Type[] types = assembly.GetTypes();

            List<PluginInfo> typeList = new List<PluginInfo>();
            Array.ForEach(types, s =>
            {
                if (!s.IsPublic || s.IsAbstract) return;
                Type t = s.GetInterface(typeof(IPlugin).FullName);
                if (t == null) return;

                object[] infos = s.GetCustomAttributes(typeof(PluginAttribute), true);

                PluginInfo info = new PluginInfo();
                info.Assembly = System.IO.Path.GetFileName(assembly.Location);
                info.TypeName = s.FullName;

                if (infos == null || infos.Length == 0)
                {
                    info.Enabled = true;
                }
                else
                {
                    info.Enabled = (infos[0] as PluginAttribute).Description.DefaultEnabled;
                }
                typeList.Add(info);
            });

            return typeList.ToArray();
        }