/// <summary> /// /// </summary> /// <param name="baseType"></param> /// <param name="directorySearched"></param> /// <param name="searchChildFolder"></param> /// <param name="config"></param> /// <returns></returns> public static IList <Type> LoadDerivedType(Type baseType, string directorySearched, bool searchChildFolder, TypeLoadConfig config) { if (config == null) { config = new TypeLoadConfig(); } IList <Type> derivedTypeList = new List <Type>(); if (searchChildFolder) { LoadDerivedTypeInAllFolder(baseType, derivedTypeList, directorySearched, config); return(derivedTypeList); } LoadDerivedTypeInOneFolder(baseType, derivedTypeList, directorySearched, config); return(derivedTypeList); }
public void LoadAllPlugins(string pluginFolderPath, bool searchChildFolder, string pluginSign) { var config = new TypeLoadConfig(CopyToMemory, false, pluginSign); IList <Type> pluginManagerTypeList = ReflectionHelper.LoadDerivedType(typeof(IPluginManager), pluginFolderPath, searchChildFolder, config); for (int i = 0; i < pluginManagerTypeList.Count; i++) { var pluginManager = (IPluginManager)Activator.CreateInstance(pluginManagerTypeList[i]); pluginManager.CopyToMemory = CopyToMemory; pluginManager.Application = Application; pluginManager.Init(); foreach (var plugin in pluginManager.PluginList) { if (_dicPlugin.ContainsKey(plugin.PluginKey)) { _dicPlugin.Remove(plugin.PluginKey); } _dicPlugin.Add(plugin.PluginKey, plugin); } pluginManagerList.Add(pluginManager); } }
public void LoadAllPlugins(string pluginFolderPath, bool searchChildFolder, string pluginSign) { var config = new TypeLoadConfig(CopyToMemory, false, pluginSign); IList <Type> pluginTypeList = ReflectionHelper.LoadDerivedType(typeof(IPlugin), pluginFolderPath, searchChildFolder, config); var pluginList = new List <IPlugin>(); for (int i = 0; i < pluginTypeList.Count; i++) { var plugin = (IPlugin)Activator.CreateInstance(pluginTypeList[i]); pluginList.Add(plugin); } pluginList.Sort(new PluginComparer()); for (int i = 0; i < pluginList.Count; i++) { IPlugin plugin = pluginList[i]; if (_dicPlugin.ContainsKey(plugin.PluginKey)) { _dicPlugin.Remove(plugin.PluginKey); } _dicPlugin.Add(plugin.PluginKey, plugin); plugin.Application = Application; } }
private static void LoadDerivedTypeInOneFolder(Type baseType, IList <Type> derivedTypeList, string folderPath, TypeLoadConfig config) { string[] files = Directory.GetFiles(folderPath); foreach (string file in files) { if (config.TargetFilePostfix != null) { if (!file.EndsWith(config.TargetFilePostfix)) { continue; } } Assembly asm = null; #region Asm try { if (config.CopyToMemory) { byte[] addinStream = FileHelper.ReadFileReturnBytes(file); asm = Assembly.Load(addinStream); } else { asm = Assembly.LoadFrom(file); } } catch (Exception ee) { ee = ee; } if (asm == null) { continue; } #endregion Type[] types = asm.GetTypes(); foreach (Type t in types) { if (t.IsSubclassOf(baseType) || baseType.IsAssignableFrom(t)) { bool canLoad = config.LoadAbstractType ? true : (!t.IsAbstract); if (canLoad) { derivedTypeList.Add(t); } } } } }
private static void LoadDerivedTypeInAllFolder(Type baseType, IList <Type> derivedTypeList, string folderPath, TypeLoadConfig config) { ReflectionHelper.LoadDerivedTypeInOneFolder(baseType, derivedTypeList, folderPath, config); string[] folders = Directory.GetDirectories(folderPath); if (folders != null) { foreach (string nextFolder in folders) { ReflectionHelper.LoadDerivedTypeInAllFolder(baseType, derivedTypeList, nextFolder, config); } } }
/// <summary> /// /// </summary> /// <param name="baseType"></param> /// <param name="derivedTypeList"></param> /// <param name="folderPath"></param> /// <param name="config"></param> private static void LoadDerivedTypeInOneFolder(Type baseType, IList <Type> derivedTypeList, string folderPath, TypeLoadConfig config) { string[] files = Directory.GetFiles(folderPath); foreach (string str in files) { if ((config.TargetFilePostfix == null) || str.EndsWith(config.TargetFilePostfix)) { Assembly assembly = null; try { if (config.CopyToMemory) { assembly = Assembly.Load(ReadFileReturnBytes(str)); } else { assembly = Assembly.LoadFrom(str); } } catch (Exception) { } if (assembly != null) { Type[] types = assembly.GetTypes(); foreach (Type type in types) { if ((type.IsSubclassOf(baseType) || baseType.IsAssignableFrom(type)) && (config.LoadAbstractType || !type.IsAbstract)) { derivedTypeList.Add(type); } } } } } }
/// <summary> /// /// </summary> /// <param name="baseType"></param> /// <param name="derivedTypeList"></param> /// <param name="folderPath"></param> /// <param name="config"></param> private static void LoadDerivedTypeInAllFolder(Type baseType, IList <Type> derivedTypeList, string folderPath, TypeLoadConfig config) { LoadDerivedTypeInOneFolder(baseType, derivedTypeList, folderPath, config); string[] directories = Directory.GetDirectories(folderPath); if (directories != null) { foreach (string str in directories) { LoadDerivedTypeInAllFolder(baseType, derivedTypeList, str, config); } } }