PlugIn über Interface
Frank Dzaebel, erstellt am: 21.04.2007, zuletzt geändert: 21.04.2007
Kategorie:Implementation, .NET-Version:2.0, [Download]

Beispiel für ein simples PlugIn über eine Schnittstelle mit Auslösung eines Events.
[Quelle]
Schnittstelle - IPluginInfo.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace PluginInterface
{
  public interface IPluginInfo
  {
    string Name { get; }
    void Ausführen();
    event EventHandler Beendet;
  }
}

PlugInTest - Program.cs
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;

using PluginInterface;

namespace PluginTest
{
  class Program
  {
    static void Main(string[] args)
    {
      string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      string dir = new FileInfo(exeDir).Name;
      exeDir = Path.GetFullPath(Path.Combine(exeDir + @"\..\..\..\PluginX\bin\", dir)); 
      ObjectHandle pluginHandle = Activator.CreateInstanceFrom( 
        Path.Combine(exeDir, "PluginX.dll"), "PluginX.PluginInfo"); 
      IPluginInfo pluginInfo = pluginHandle.Unwrap() as IPluginInfo;
      pluginInfo.Beendet += new EventHandler(pluginInfo_Beendet); 
      Console.WriteLine("Der Name des PlugIn's ist: " + pluginInfo.Name); 
      Console.WriteLine("Ausführen ausgelöst (bitte warten) ...");
      pluginInfo.Ausführen(); 
    } 
    
    static void pluginInfo_Beendet(object sender, EventArgs e) 
    { 
      Console.WriteLine("Beendet! (bitte Enter drücken)");
      Console.ReadLine();
    }
  }
}

PluginX - PluginInfo.cs
using System;
using System.Collections.Generic;
using System.Text;

using PluginInterface;
using System.Threading;

namespace PluginX
{
  public class PluginInfo : IPluginInfo
  {
    public string Name
    {
      get { return "Plugin X"; }
    }

    public event EventHandler Beendet;
    public void Ausführen()
    {
      Thread.Sleep(3000);
      Beendet(this, EventArgs.Empty);
    }
  }
}