/// <summary> /// The inject property dependencies. /// </summary> /// <param name="plugin"> /// The plugin. /// </param> /// <param name="coreInst"> /// The core inst. /// </param> /// <param name="pluginMgr"> /// The plugin mgr. /// </param> /// <param name="sessionGuid"> /// The session guid. /// </param> /// <param name="isDevelopment"> /// The is development. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool InjectPropertyDependencies(IPluginBase plugin, ICore coreInst, IPluginManager <ICore> pluginMgr, Guid sessionGuid, bool isDevelopment) { bool coreSet = false; bool mgrSet = false; bool guidSet = false; var type = plugin.GetType(); while (type != null && type != typeof(IPluginBase)) { var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var prop in props) { if (CoreInterfaceTypes.Contains(prop.PropertyType) && prop.CanWrite) { prop.SetValue(plugin, coreInst /*Convert.ChangeType(coreInst, prop.PropertyType)*/); coreSet = true; } else if (PluginMgrInterfaceTypes.Contains(prop.PropertyType) && prop.CanWrite) { prop.SetValue(plugin, pluginMgr /*Convert.ChangeType(pluginMgr, prop.PropertyType)*/); mgrSet = true; } else if (prop.PropertyType == typeof(Guid) && prop.CanWrite) { prop.SetValue(plugin, sessionGuid); guidSet = true; } else if (prop.PropertyType == typeof(bool) && prop.Name is "IsDevelopmentPlugin" && prop.CanWrite) { prop.SetValue(plugin, isDevelopment); } } type = type.BaseType; } return(coreSet && mgrSet && guidSet); }
public bool LoadPlugin(string pluginEntryPointDllFullFilename) { Debug($"当前AppDomain:{AppDomain.CurrentDomain.FriendlyName},开始加载插件程序集:{pluginEntryPointDllFullFilename}"); try { Assembly.Load(new AssemblyName { CodeBase = pluginEntryPointDllFullFilename, }); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { Debug($"当前已加载程序集:{assembly.FullName}"); } Debug($"程序集加载完毕,开始构建Container"); var superBuilder = new ContainerBuilder(); superBuilder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).AsImplementedInterfaces() .AsSelf(); var superContainer = superBuilder.Build(); var subBuilderRegisters = superContainer.Resolve<IAutofacContainerBuilder[]>().ToArray(); var builder = new ContainerBuilder(); builder.Register(x => new CoolQApi()).AsImplementedInterfaces().SingleInstance(); builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).AsImplementedInterfaces() .AsSelf(); foreach (var autofacContainerBuilder in subBuilderRegisters) { autofacContainerBuilder.Register(builder); } var container = builder.Build(); Debug($"Container构建完毕,尝试获取{nameof(IPluginBase)}实现类"); var impls = container.Resolve<IEnumerable<IPluginBase>>().ToArray(); Debug($"实现类一共{impls.Length}个"); _pluginBase = impls.First(x => !(x is CrossAppDomainPluginLoader)); Debug($"获取到了{_pluginBase.GetType().Name}作为{nameof(IPluginBase)}的实现类,插件加载完毕"); return true; } catch (Exception ex) { Message = ex.Message; return false; } }
/// <summary> /// If the class implements the ExportJarsPlugin (or inherited from) attribute, this function will extract the name of the plugin given in the Attribute. /// </summary> /// <returns></returns> public static string GetPluginTextFromAttributeValue(this IPluginBase plugin) { IPluginMetadata attribute = plugin.GetType().GetCustomAttributes <ExportPluginAttribute>(false).FirstOrDefault(); return((attribute != null) ? attribute.PluginText : plugin.GetType().Name); }