[C#] Plugin Framework - Statisches einbinden auf dynamisch ändern

Halfbax

Erfahrenes Mitglied
Guten Tag,

ich habe wage mich gerade an das schreiben eines Plug-in Managers aus Übungszwecken, aber ich bleibe bei den Punkt des dynamischen einbinden hängen.

Das Laden sowie Ausführen des Plug-ins funktioniert tadellos.

Ich möchte gerne das einbinden meiner Plugins, sofern ich das richtig nachgelesen habe, über das Interface IConfigurationSectionHandler durchführen. Dazu habe diesen implementiert und er liest theoretisch meine Plugins aus der App.config Datei. Nun stellt sich die Frage wie rufe ich die .Create() Methode auf?

App.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
      <section name="plugins"
            type="Plug_in_Framework_Testversion1.PluginSectionHandler, Plug_in_Framework_Testversion1"
              />
    </configSections> 
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <plugins>
      <plugin type="Plug_in_Framework_Testversion1.EmailPlugin, EmailPlugin" />
    </plugins>
 
</configuration>

MainWindows.xaml.cs - HIER WIRD DAS PLUGIN STATISCH EINGEBUNDEN
Code:
        private void ExecutePlugin(PluginManager plugin)
        {
            EditorContext context = new EditorContext(noticeBlock.Text, (SolidColorBrush)noticeBlock.Background);

            plugin.PerformAction(context);
            noticeBlock.Text = context.CurrentDocumentText;
            noticeBlock.Background = context.DocumentBackgroundColor;
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            string classname = "Plug_in_Framework_Testversion1.EmailPlugin, EmailPlugin";
            PluginManager plugin = (PluginManager)Activator.CreateInstance(Type.GetType(classname));
            ExecutePlugin(plugin);
        }

PluginManager.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Plug_in_Framework_Testversion1
{
    public interface PluginManager
    {
        string Name { get; }
        void PerformAction(PluginManagerContext context);
    }

    public interface PluginManagerContext
    {
        string CurrentDocumentText { get; }
        SolidColorBrush DocumentBackgroundColor { get; set; }
    }

    public class EditorContext : PluginManagerContext
    {
        private string m_CurrentText = string.Empty;
        private SolidColorBrush m_CurrentColor = Brushes.White;

        public EditorContext(string CurrentEditorText, SolidColorBrush CurrentEditorColor)
        {
            m_CurrentText = CurrentEditorText;
            m_CurrentColor = CurrentEditorColor;
        }

        public string CurrentDocumentText
        {
            get { return m_CurrentText; }
        }

        public SolidColorBrush DocumentBackgroundColor
        {
            get { return m_CurrentColor; }
            set { m_CurrentColor = value; }
        }
    }
}

PluginManagerCSHandler.cs
Code:
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Xml;

namespace Plug_in_Framework_Testversion1
{
    class PluginSectionHandler : IConfigurationSectionHandler
    {
        public PluginSectionHandler()
        {
        }

        public object Create(object parent, object configContext, XmlNode section)
        {
            Collection<PluginManager> plugins = new Collection<PluginManager>();

            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    object plugObject =
                                Activator.CreateInstance(Type.GetType(node.Attributes["type"].Value));

                    //Cast this to an PluginManager interface and add to the collection
                    PluginManager plugin = (PluginManager)plugObject;
                    plugins.Add(plugin);
                }
                catch (Exception e)
                {
                    //Catch any exceptions
                }
            }
            return plugins;
        }
    }
}
 

Neue Beiträge

Zurück