我已經做了很多工作,像這樣,我得到了幾乎所有我neeed從這裏開始的信息:
http://www.codeproject.com/KB/cs/pluginsincsharp.aspx
我最大的項目使用的插件是一個音頻處理應用。
的總體思路是:
您要爲每個你在一個單獨的,分佈的,簽署DLL需要插件類型創建一個接口。下面是一個例子接口:
/// <summary>
/// Describes an audio reader.
/// </summary>
public interface IReader : IPlugin
{
/// <summary>
/// Fired by the reader to update the UI on long operations.
/// </summary>
event EventHandler<ProgressChangedEventArgs> ProgressChanged;
/// <summary>
/// Gets a list of of MIME types the reader supports.
/// </summary>
ReadOnlyCollection<string> TypesSupported { get; }
/// <summary>
/// Loads a SampleSet from the given input stream.
/// </summary>
/// <param name="inStream">The stream to read the</param>
/// <returns>A SampleSet read from the stream.</returns>
SampleSet Load(Stream inStream);
}
然後在你的插件DLL文件,需要實現與接口的類:
public sealed class WavReader : IReader
{
...
}
然後你使用反射加載DLL:
private void LoadPlugins(string applicationPath)
{
if (!Directory.Exists(applicationPath))
throw new ArgumentException("The path passed to LoadPlugins was not found.", "applicationPath");
List<IPluginFactory> factoriesList = new List<IPluginFactory>();
List<IReader> readersList = new List<IReader>();
foreach (string assemblyFilename in Directory.GetFiles(applicationPath, "*.plugin"))
{
Assembly moduleAssembly = Assembly.LoadFile(Path.GetFullPath(assemblyFilename));
foreach (Type type in moduleAssembly.GetTypes())
{
IPluginFactory instance = null;
if (type.GetInterface("IPluginFactory") != null)
instance = (IPluginFactory)Activator.CreateInstance(type);
if (instance != null)
{
factoriesList.Add(instance);
switch (instance.PluginType)
{
case PluginType.Reader:
readersList.Add((IReader)instance.Create());
break;
}
}
}
}
this.readers = readersList.AsReadOnly();
}
而且BAM你有你的插件!