public void LoadFrom(string path) { //get path to libs folder string libsPath = path; //get only dll files string[] providers = Directory.GetFiles(libsPath, "*.dll"); //for simplicity excaped LINQ query... //for every provider .... foreach (string provider in providers) { //load it into application RAM.. Assembly assembly = Assembly.LoadFile(provider); //get all types in assembly Type[] assemblyTypes = assembly.GetTypes(); foreach (Type assemblyType in assemblyTypes) { ProviderNameAttribute providerNameAttr = (ProviderNameAttribute)assemblyType.GetCustomAttribute(typeof(ProviderNameAttribute)); var method = assemblyType.GetMethods() .Where(x => x.GetCustomAttribute(typeof(ProviderOperationAttribute)) != null) .FirstOrDefault(); //if current type implemented IProvider interface then.. if (providerNameAttr != null && method != null) { object instance = Activator.CreateInstance(assemblyType); var providerOperationAttr = method.GetCustomAttribute <ProviderOperationAttribute>(); this.AddProvider(providerNameAttr.ProviderName, () => { method.Invoke(instance, null); }, providerOperationAttr.EventType); } } } }
static DbManagerFactory() { // Register assemblies string contentRootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string[] assemblies = (contentRootPath != null) ? Directory.GetFiles(contentRootPath, "Kit.Dal.*.dll", SearchOption.TopDirectoryOnly) : new string[] {}; Func <Type, bool> pre = t => t.GetInterfaces().Contains(typeof(IDbManager)); Managers = new Dictionary <string, Type>(); foreach (string a in assemblies) { #if NETCOREAPP1_0 Assembly assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(a); #endif #if NET46 Assembly assembly = Assembly.LoadFrom(a); #endif foreach (Type t in assembly.GetTypes().Where(pre)) { // Наименование --> из аттрибута ProviderNameAttribute attr = null; #if NETCOREAPP1_0 attr = (ProviderNameAttribute)t.GetTypeInfo().GetCustomAttribute(typeof(ProviderNameAttribute)); #endif #if NET46 attr = (ProviderNameAttribute)t.GetCustomAttribute(typeof(ProviderNameAttribute)); #endif if (attr != null) { Managers[attr.ProviderName] = t; } } } }