public ConsumerPlugin(IProducerPlugin producer) { Type consumerType = this.GetType(); Type providerType = producer.GetType(); ConsoleLog.WriteAssemblyInformation("ConsumerPlugin - Constructor", consumerType, providerType); }
/// <summary> /// Creates an instance of Plugin-Item, based on a producer. /// </summary> /// <param name="plugin">Producer-Plugin</param> /// <param name="writeCustomStates">List of custom states for write-access</param> /// <param name="readCustomStates">List of custom states for read-access</param> public PluginItem(IProducerPlugin plugin, CustomStateItem[] writeCustomStates, CustomStateItem[] readCustomStates) : this(writeCustomStates, readCustomStates, plugin) { // Check for null if (plugin == null) { throw new ArgumentNullException("plugin", Resource.PluginItemConstructorPluginIsNull); } producer = plugin; }
public void SetProducer(IProducerPlugin producer) { Type newProducerType = producer.GetType(); bool typeCheckResult = newProducerType == expectedProducerType; ConsoleLog.WriteAssemblyInformation( $"ConsumerPlugin - SetProducer - Type check result: {typeCheckResult} - [expected, provided]", expectedProducerType, newProducerType); }
static void Main(string[] args) { string producerPluginsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins/producer"); string consumerPluginsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins/consumer"); PluginRegistry pluginRegistry = new PluginRegistry(producerPluginsPath, consumerPluginsPath); IProducerPlugin producerPlugin = pluginRegistry.GetPlugin <IProducerPlugin>("ProducerPlugin.ProducerPlugin"); IConsumerPlugin consumerPlugin = pluginRegistry.GetPlugin <IConsumerPlugin>("ConsumerPlugin.ConsumerPlugin"); consumerPlugin.SetProducer(producerPlugin); }
/// <summary> /// search in given assembly for a new plugin /// </summary> /// <param name="assembly">assembly to search in</param> /// <returns>true, if there are valid plugins inside</returns> private bool addPlugin(Assembly assembly) { bool hit = false; // Get all includes Types foreach (Type type in assembly.GetExportedTypes()) { // Find the attribute List <CustomStateItem> readCustomStates = new List <CustomStateItem>(); List <CustomStateItem> writeCustomStates = new List <CustomStateItem>(); foreach (CustomAttributeData customAttribute in CustomAttributeData.GetCustomAttributes(type)) { string name; string dataType; string description; switch (customAttribute.Constructor.ReflectedType.FullName) { case "AntMe.SharedComponents.Plugin.ReadCustomStateAttribute": name = string.Empty; dataType = string.Empty; description = string.Empty; foreach (CustomAttributeNamedArgument argument in customAttribute.NamedArguments) { switch (argument.MemberInfo.Name) { case "Name": name = (string)argument.TypedValue.Value; break; case "Type": dataType = (string)argument.TypedValue.Value; break; case "Description": description = (string)argument.TypedValue.Value; break; } } readCustomStates.Add(new CustomStateItem(name, dataType, description)); break; case "AntMe.SharedComponents.Plugin.WriteCustomStateAttribute": name = string.Empty; dataType = string.Empty; description = string.Empty; foreach (CustomAttributeNamedArgument argument in customAttribute.NamedArguments) { switch (argument.MemberInfo.Name) { case "Name": name = (string)argument.TypedValue.Value; break; case "Type": dataType = (string)argument.TypedValue.Value; break; case "Description": description = (string)argument.TypedValue.Value; break; } } writeCustomStates.Add(new CustomStateItem(name, dataType, description)); break; } } // If type has an attribute, search for the interfaces foreach (Type plugin in type.GetInterfaces()) { // Producer found if (plugin == typeof(IProducerPlugin)) { // Create an instance of plugin and add to list PluginItem item = null; try { IProducerPlugin producerPlugin = (IProducerPlugin)Activator.CreateInstance(type, false); item = new PluginItem(producerPlugin, writeCustomStates.ToArray(), readCustomStates.ToArray()); hit = true; } catch (Exception ex) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerProducerPluginCommonProblems, type.FullName, assembly.GetFiles()[0].Name), ex)); } // Warnings, of there is another Version of that plugin if (item != null && producerList.ContainsKey(item.Guid)) { if (producerList[item.Guid].Version > item.Version) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerProducerPluginNewerVersionLoaded, item.Name, item.Version))); item = null; } else if (producerList[item.Guid].Version < item.Version) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerProducerPluginNewerVersion, item.Name, item.Version))); DeactivateProducer(item.Guid); producerList.Remove(item.Guid); } else { // Samle plugin still loaded item = null; } } // add to list if (item != null) { // Check, if plugin is preselected or saved as selected producerList.Add(item.Guid, item); if (config.selectedPlugins.Contains(item.Guid) || (!config.loaded && type.GetCustomAttributes(typeof(PreselectedAttribute), false).Length > 0)) { ActivateProducer(item.Guid); } // Load Settings if (File.Exists(configPath + item.Guid + Resources.PluginSettingsFileExtension)) { try { item.Producer.Settings = File.ReadAllBytes( configPath + item.Guid + Resources.PluginSettingsFileExtension); } catch (Exception ex) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerProducerPluginSettingsLoadFailed, item.Name, item.Version), ex)); } } } } // Consumer found else if (plugin == typeof(IConsumerPlugin)) { // Create an instance of plugin and add to list PluginItem item = null; try { IConsumerPlugin consumerPlugin = (IConsumerPlugin)Activator.CreateInstance(type, false); item = new PluginItem(consumerPlugin, writeCustomStates.ToArray(), readCustomStates.ToArray()); hit = true; } catch (Exception ex) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerConsumerPluginCommonProblems, type.FullName, assembly.GetFiles()[0].Name), ex)); } // Warnings, of there is another Version of that plugin if (item != null && consumerList.ContainsKey(item.Guid)) { if (consumerList[item.Guid].Version > item.Version) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerConsumerPluginNewerVersionLoaded, item.Name, item.Version))); item = null; } else if (consumerList[item.Guid].Version < item.Version) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerConsumerPluginNewerVersion, item.Name, item.Version))); DeactivateConsumer(item.Guid); consumerList.Remove(item.Guid); } else { // Same plugin still loaded item = null; } } // add to list if (item != null) { consumerList.Add(item.Guid, item); // Check, if plugin is preselected or saved as selected if (config.selectedPlugins.Contains(item.Guid) || (!config.loaded && type.GetCustomAttributes(typeof(PreselectedAttribute), false).Length > 0)) { ActivateConsumer(item.Guid); } // Load Settings if (File.Exists(configPath + item.Guid + Resources.PluginSettingsFileExtension)) { try { item.Consumer.Settings = File.ReadAllBytes( configPath + item.Guid + Resources.PluginSettingsFileExtension); } catch (Exception ex) { exceptions.Add( new Exception( string.Format( Resource.PluginManagerConsumerPluginSettingsLoadFailed, item.Name, item.Version), ex)); } } } } } } return(hit); }