Exemplo n.º 1
0
 public bool LoadAssembly(string Path)
 {
     try
     {
         if (!DllPathDic.Contains(Path))
         {
             Assembly    assembly = Assembly.LoadFrom(Path);
             List <Type> types    = assembly.GetTypes().ToList();
             //合并去重字典缓存
             ControllerDic = ControllerDic == null ? new Dictionary <string, Type>() : ControllerDic;
             ControllerDic = ControllerDic.Union(types.Where(w => w.GetCustomAttribute(typeof(DescriptionAttribute), false) != null && ((DescriptionAttribute)w.GetCustomAttribute(typeof(DescriptionAttribute), false)).Description == "Controller").ToDictionary(type => type.Name.ToLower(), type => type)).ToDictionary(t => t.Key, t => t.Value);
             foreach (var t in types)
             {
                 Attribute attr = t.GetCustomAttribute(typeof(DescriptionAttribute), false);
                 if (attr != null && ((DescriptionAttribute)attr).Description == "Controller")
                 {
                     foreach (var m in t.GetMethods())
                     {
                         if (!ActionDic.ContainsKey(m.Name.ToLower()))
                         {
                             ActionDic.Add(m.Name.ToLower(), m);
                         }
                     }
                 }
                 //ActionDic = ActionDic.Union(t.GetMethods().ToDictionary(m => m.Name.ToLower(), m => m)).ToDictionary(m => m.Key, m => m.Value);
             }
             //初始化插件数据结构
             InitControllersData();
             DllPathDic.Add(Path);
             return(true);
         }
         else
         {
             throw new Exception("程序集重复加载!");
         }
     }
     catch (Exception e)
     {
         Log.Write(e.ToString());
         throw e;
     }
 }
Exemplo n.º 2
0
 private object GetControllerInstance(string Controller)
 {
     if (ControllerDic.ContainsKey(Controller))
     {
         object obj = null;
         if (InstanceDic.ContainsKey(ControllerDic[Controller]))
         {
             obj = InstanceDic[ControllerDic[Controller]];
         }
         else
         {
             obj = Activator.CreateInstance(ControllerDic[Controller]);
             InstanceDic.Add(ControllerDic[Controller], obj);
         }
         return(obj);
     }
     else
     {
         throw new Exception("Controller not found!");
     }
 }