myblog设计说明之 Plug架构....(原创)

    只写代码,说明稍后补上:
    载入插件目录中用户上传的DLL

   
     private static void LoadPlugs()//分析载入dll里的web控件缓存
        {

            string[] files = Directory.GetFiles(Globals.PlugsPath, "*.dll");
            Cache cache = HttpRuntime.Cache;

            Dictionary<string, Type> plugDict = new Dictionary<string, Type>();
            foreach (string f in files) //查找所有dll文件
            {

                try
                {
                    Assembly a = Assembly.LoadFrom(f);//载入

                    System.Type[] types = a.GetTypes();
                    foreach (System.Type type in types)
                    {


                        if (type.IsSubclassOf(typeof(WebControl)))//查找是否 WebControl
                        {
                            if (type.GetCustomAttributes(typeof(PlugDisplayNameAttribute), false).Length != 1) //应用的属性                                throw new PlugException(PlugExceptionType.PlugDisplayNameNotFound, type);
                            else
                            {
                                     PlugAttribute att=
                 (PlugAttribute)PlugAttribute.GetCustomAttribute(type,typeof(PlugAttribute));
                                plugDict.Add(att.Name, type);  //增加到字典
                            }
                        }
                      
                    }

                    cache.Insert("myblog_Plug", plugDict);  //加入缓存

                }
                catch (PlugException e)
                {
                    throw;
                }
                catch (Exception e)
                {

                    throw ;

                }
            }

            return;
        }

 载入Plug 返回控件引用:

 public static Control LoadPlug(string className)
        { 
            Dictionary<string, Type> dict = (Dictionary<string, Type>)HttpRuntime.Cache["myblog_Plug"];
            if (dict == null)
            {
              LoadPlugs();
              dict = HttpRuntime.Cache["myblog_Plug"] as Dictionary<string, Type>;
            }     //从缓存获取插件
            if (dict.ContainsKey(className) || dict[className] != null)
            {
                Type t = dict[className];
                t.GetConstructor(Type.EmptyTypes).Invoke(null); //查找不带参的构造函数
                return (Control)t.GetConstructor(Type.EmptyTypes).Invoke(null);  //调用返回  
            }
            return null;
        }

重载一个 ,处理构造函数多参情况

  public static Control LoadPlug(string className, params object[] args)
        {
            Dictionary<string, Type> dict = (Dictionary<string, Type>)HttpRuntime.Cache["myblog_Plug"];
            if (dict == null)
            {
                LoadPlugs();
                dict = HttpRuntime.Cache["myblog_Plug"] as Dictionary<string, Type>;
            }
            if (dict.ContainsKey(className) || dict[className] != null)
            {
                Type t = dict[className];
                Type[] ots=new Type[args.Length];
                for (int i = 0; i < ots.Length; i++)
                {
                 ots[i]=typeof(string);
                }
                if(t.GetConstructor(ots)!=null) //查找带  ots.Length;个参数的构造方法
               return (Control)t.GetConstructor(ots).Invoke(args); //返回引用
            }
            return null;
        }


属性定义 :
 [AttributeUsage(AttributeTargets.Class)]
    public class PlugAttribute : System.Attribute
    {
        private string _title;
        private string _name;
        public PlugAttribute(string name,string title)
            : base()
        {
         
            _title = title;
            _name = name;
            return;

        }
        public string Title
        {
            get { return _title; }
        }
        public string Name
        {
            get { return _name; }
        }
        public PlugAttribute(string name, string title,string des)
            : base()
        {

            _title = title;
            _name = name;
            return;

        }

        public override string ToString()
        {
            return _title;
        }

    }

 待续..

posted @ 2006-05-27 03:46  潇笑  阅读(396)  评论(1编辑  收藏  举报