protected void Page_Load(object sender, EventArgs e) { if (!this.Page.IsPostBack) { var config = EmailConfigInfo.Current; smtp.Text = config.Smtp; port.Text = config.Port.ToString(); sysemail.Text = config.Sysemail; userName.Text = config.Username; password.Text = config.Password; try { // 加载所有邮件插件 foreach (var item in AssemblyX.FindAllPlugins(typeof(ISmtpMail))) { var des = item.GetCustomAttribute <DescriptionAttribute>(true); var name = des == null ? item.FullName : des.Description; smtpemail.Items.Add(new ListItem(name, item.AssemblyQualifiedName)); } } catch { } try { smtpemail.SelectedValue = config.PluginNameSpace + "," + config.DllFileName; } catch { } } }
IEnumerable <Type> LoadPlugins() { if (pluginTypes != null) { return(pluginTypes); } var list = new List <Type>(); // 此时是加载所有插件,无法识别哪些是需要的 foreach (var item in AssemblyX.FindAllPlugins(typeof(IPlugin), true)) { if (item != null) { // 如果有插件特性,并且所有特性都不支持当前宿主,则跳过 var atts = item.GetCustomAttributes <PluginAttribute>(true); if (atts != null && atts.Any(a => a.Identity != Identity)) { continue; } list.Add(item); } } return(pluginTypes = list); }
/// <summary>遍历所有引用了AreaRegistrationBase的程序集</summary> /// <returns></returns> static List <Assembly> FindAllArea() { var list = new List <Assembly>(); foreach (var item in AssemblyX.FindAllPlugins(typeof(AreaRegistrationBase), true)) { var asm = item.Assembly; if (!list.Contains(asm)) { list.Add(asm); //yield return asm; } } // 为了能够实现模板覆盖,程序集相互引用需要排序,父程序集在前 list.Sort((x, y) => { if (x == y) { return(0); } if (x != null && y == null) { return(1); } if (x == null && y != null) { return(-1); } return(x.GetReferencedAssemblies().Any(e => e.FullName == y.FullName) ? 1 : -1); }); return(list); }
public EntityModules(Type entityType) { EntityType = entityType; // 扫描添加 foreach (var item in AssemblyX.FindAllPlugins(typeof(IEntityModule), true)) { var module = item.CreateInstance() as IEntityModule; Add(module); } }
static MallPluginProvider() { //try //{ // _sp = (MallPluginBase)Activator.CreateInstance(Type.GetType("BBX.Mall.MallPlugin, BBX.Mall", false, true)); //} //catch //{ // _sp = null; //} foreach (var item in AssemblyX.FindAllPlugins(typeof(MallPluginBase), true)) { _sp = TypeX.CreateInstance(item) as MallPluginBase; break; } }
static SpacePluginProvider() { //try //{ // _sp = (SpacePluginBase)Activator.CreateInstance(Type.GetType("Discuz.Space.SpacePlugin, Discuz.Space", false, true)); //} //catch //{ // _sp = null; //} foreach (var item in AssemblyX.FindAllPlugins(typeof(SpacePluginBase), true)) { _sp = TypeX.CreateInstance(item) as SpacePluginBase; break; } }
/// <summary>初始化</summary> static void Init() { var container = ObjectContainer.Current; var asm = Assembly.GetExecutingAssembly(); // 搜索已加载程序集里面的消息类型 foreach (var item in AssemblyX.FindAllPlugins(typeof(Message), true)) { var msg = TypeX.CreateInstance(item) as Message; if (msg != null) { if (item.Assembly != asm && msg.Kind < MessageKind.UserDefine) { throw new XException("不允许{0}采用小于{1}的保留编码{2}!", item.FullName, MessageKind.UserDefine, msg.Kind); } container.Register(typeof(Message), null, msg, msg.Kind); } //if (msg != null) container.Register<Message>(msg, msg.Kind); } }
public override bool Start() { base.Start(); return(false); try { //CreateAllTransport(); var types = AssemblyX.FindAllPlugins(typeof(IWork), true); //var types1=AssemblyX.FindAllPlugins("typeof()") _works = types.Select(t => (IWork)TypeX.CreateInstance(t)); int count = 0; foreach (var work in _works) { count++; } ServiceLogger.Current.WriteDebugLog("加载工作IWork插件{0}个:{1}", count, _works.Select(iw => iw.GetType().Name).Join(",")); } catch (Exception ex) { ServiceLogger.Current.WriteError("加载工作插件IWork失败"); ServiceLogger.Current.WriteException(ex); return(false); } if (_works != null && _works.Any()) { _works.ForEach(iw => { iw.Start(); ServiceLogger.Current.WriteLog("{0}开始工作", iw.GetType().Name); }); } return(true); }
/// <summary> /// 初始化 /// </summary> private static void Init() { var container = ObjectContainer.Current; var asm = Assembly.GetExecutingAssembly(); // 搜索已加载程序集里面的消息类型 foreach (var item in AssemblyX.FindAllPlugins(typeof(IRequest), true)) { var msg = TypeX.CreateInstance(item) as IRequest; if (msg != null) { container.Register(typeof(IRequest), item, null, msg.Function); } } foreach (var item in AssemblyX.FindAllPlugins(typeof(IResponse), true)) { var msg = TypeX.CreateInstance(item) as IResponse; if (msg != null) { container.Register(typeof(IResponse), item, null, msg.Function); } } }
/// <summary>遍历所有程序集的所有类型,自动注册实现了指定接口或基类的类型。如果没有注册任何实现,则默认注册第一个排除类型</summary> /// <param name="from">接口或基类</param> /// <param name="getidCallback">用于从外部类型对象中获取标识的委托</param> /// <param name="id">标识</param> /// <param name="priority">优先级</param> /// <param name="excludeTypes">要排除的类型,一般是内部默认实现</param> /// <returns></returns> public virtual IObjectContainer AutoRegister(Type from, Func <Object, Object> getidCallback = null, Object id = null, Int32 priority = 0, params Type[] excludeTypes) { if (from == null) { throw new ArgumentNullException("from"); } if (excludeTypes == null) { excludeTypes = Type.EmptyTypes; } // 如果存在已注册项,并且优先级大于0,那么这里就不要注册了 var dic = Find(from, false); if (dic != null) { var map = FindMap(dic, null, false) as Map; if (map != null && map.Priority > 0) { return(this); } } IEnumerable <Type> listType = AssemblyX.FindAllPlugins(from, true); // 遍历所有程序集,自动加载 foreach (var item in AssemblyX.FindAllPlugins(from, true)) { if (Array.IndexOf(excludeTypes, item) < 0) { // 自动注册的优先级是1,高于默认的0 //Register(from, item, null, null, 1); // 实例化一次,让这个类有机会执行类型构造函数,可以获取旧的类型实现 var obj = TypeX.CreateInstance(item); // 如果指定了获取ID的委托,并且取得的ID与传入ID不一致,则不承认 if (getidCallback != null && id != getidCallback(obj)) { continue; } if (XTrace.Debug) { XTrace.WriteLine("为{0}自动注册{1},标识={2},优先级={3}!", from.FullName, item.FullName, id, priority + 1); } Register(from, null, obj, id, priority + 1); return(this); } } // 如果没有注册任何实现,则默认注册第一个排除类型 if (excludeTypes.Length > 0) { //if (dic == null) { Register(from, excludeTypes[0], null, id, priority); } } return(this); }
private static IEnumerable <IDataSync> InitPlugins() { var ps = AssemblyX.FindAllPlugins(typeof(IDataSync), true); return(ps.Select(e => (IDataSync)TypeX.CreateInstance(e))); }
/// <summary>获取指定连接名下的所有实体类</summary> /// <param name="connName"></param> /// <param name="isLoadAssembly">是否从未加载程序集中获取类型。使用仅反射的方法检查目标类型,如果存在,则进行常规加载</param> /// <returns></returns> public static IEnumerable <Type> LoadEntities(String connName, Boolean isLoadAssembly) { return(AssemblyX.FindAllPlugins(typeof(IEntity), isLoadAssembly).Where(t => TableItem.Create(t).ConnName == connName)); }
/// <summary>列出所有实体类</summary> /// <returns></returns> public static List <Type> LoadEntities() { return(AssemblyX.FindAllPlugins(typeof(IEntity)).ToList()); }