• 插件开发


        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;
            }
        }
  • 相关阅读:
    FreeRTOS计数型信号量
    FreeRTOS二值信号量
    FreeRTOS队列操作
    ROMTableAddr = 0xE00FF003 错误 Target DLL has been cancelled 错误
    stm32 USART_IT_IDLE中断 一帧数据
    Moving to Express 4
    node.js 模板 ejs 转
    [译]JavaScript中,{}+{}等于多少?
    mongoose简单使用样例
    MongoVUE简单操作手册
  • 原文地址:https://www.cnblogs.com/ly45/p/6918690.html
Copyright © 2020-2023  润新知