插件开发
Posted 大胡子毛绒老头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插件开发相关的知识,希望对你有一定的参考价值。
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; } }
以上是关于插件开发的主要内容,如果未能解决你的问题,请参考以下文章