private static PluginPackAttribute GetAttributeByType(Type type) { //int key = type.Assembly.GetHashCode(); //if (attrDicts == null) //{ // attrDicts = new Dictionary<int, PluginPackAttribute>(); //} //if (!attrDicts.Keys.Contains(key)) //{ object[] attrs = type.Assembly.GetCustomAttributes(typeof(PluginPackAttribute), false); if (attrs.Length == 0) { throw new Exception("assembly not contain any plugin package attribute!"); } PluginPackAttribute attr = (PluginPackAttribute)attrs[0]; //attrDicts.Add(key, attr); return(attr); //} // return attrDicts[key]; }
/// <summary> /// 获取插件 /// </summary> /// <param name="workerIndent"></param> /// <returns></returns> public static IPlugin GetPlugin(string workerIndent, out PluginPackAttribute attr) { var plugins = BasePluginHost.plugins; foreach (IPlugin p in plugins.Keys) { if (String.Compare(workerIndent, plugins[p].WorkIndent, StringComparison.OrdinalIgnoreCase) == 0) { attr = plugins[p]; return(p); } } attr = null; return(null); }
/// <summary> /// 注册扩展处理程序 /// </summary> /// <param name="extendName">扩展名称,而且也是访问地址的名称。如扩展名称为:ext,那么可使用/ext.sh访问该扩展插件</param> /// <param name="getReqHandler">委托PluginHandler<CmsContext,string>的实例</param> /// <param name="postReqHandler">委托PluginHandler<CmsContext,string>的实例</param> /// <returns></returns> public bool Register(IPlugin plugin, PluginHandler <T> reqHandler) { Type type = plugin.GetType(); PluginPackAttribute attr = PluginUtil.GetAttribute(plugin); string indent = attr.WorkIndent; if (reqHandler == null || reqHandlers.Keys.Contains(indent)) { return(false); } if (reqHandler != null) { reqHandlers.Add(indent, reqHandler); } return(true); }
/// <summary> /// 从程序集中加载插件 /// </summary> /// <param name="assembly"></param> /// <returns></returns> private static bool loadFromAssembly(Assembly assembly) { PluginPackAttribute attribute = null; IPlugin plugin = null; var attbs = assembly.GetCustomAttributes(typeof(PluginPackAttribute), false); if (attbs.Length != 0) { attribute = (PluginPackAttribute)attbs[0]; } else { // foreach (Attribute attr in assembly.GetCustomAttributes(false)) // { // if (attr.ToString().IndexOf("PluginPackAttribute") != -1) // { // attribute = (PluginPackAttribute) attr; // // Log.Println(attribute.Name); // } // else // { // Log.Println(attr.ToString()); // } // } // // Log.Println(assembly.GetName().Name+" not a plugin!"); return(false); } var types = assembly.GetTypes(); foreach (Type type in types) { if (type.IsClass) { foreach (Type t in type.GetInterfaces()) { if (t == typeof(IPlugin)) { plugin = Activator.CreateInstance(type) as IPlugin; if (plugin == null) { continue; } if (attribute != null) { plugins.Add(plugin, attribute); } } } } } if (Log != null) { Log.Println(String.Format("{0}({1}) be found ; version is {2}.", attribute.Name, assembly.GetName().Name, attribute.Version)); } return(true); }