我已经做了很多工作,像这样,我得到了几乎所有我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你有你的插件!