首页 > 代码库 > 插件开发
插件开发
public class Loader { public IEnumerable<Type> AllTypes { get; set; } private List<object> allInstances = new List<object>(); public event EventHandler<UnhandledExceptionEventArgs> LoadingError; public void LoadFromFolder(string folder) => AllTypes = Directory.GetFiles(folder, "*.dll").SelectMany(f => { IEnumerable<Type> ts; try { ts = Assembly.LoadFile(f).ExportedTypes; } catch { return Enumerable.Empty<Type>(); } return ts; }); public T Instance<T>() => Instances<T>(false, t => true).FirstOrDefault(); public IEnumerable<T> Instances<T>() => Instances<T>(false, t => true); public T Instance<T>(bool createNew, Func<T, bool> filter) => Instances(createNew, filter).FirstOrDefault(); public IEnumerable<T> Instances<T>(bool createNew, Func<T, bool> filter) { var bt = typeof(T); var ii = bt.IsInterface; return from type in AllTypes where !type.IsInterface && !type.IsAbstract && (ii ? type.GetInterfaces().Contains(bt) : type.IsSubclassOf(bt)) let r = (T)create(type, createNew) where filter(r) select r; } private object create(Type type, bool createNew) { var r = allInstances.FirstOrDefault(i => i.GetType() == type) ?? Activator.CreateInstance(type); if (createNew) { allInstances.Remove(r); } allInstances.Add(r); return r; } }
插件开发
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。