我想提供一种在我的软件中创建动态可加载插件的方式。通常的方法是使用 LoadLibrary WinAPI 函数来加载 DLL 文件,然后调用 GetProcAddress 函数获取该 DLL 中某个函数的指针。
我的问题是如何在 C#/.Net 应用程序中动态加载插件?
我的问题是如何在 C#/.Net 应用程序中动态加载插件?
从.NET 3.5开始,有一种正式且内置的方式来创建和从.NET应用程序中加载插件。它都在System.AddIn命名空间中。想了解更多信息,可以查看MSDN上的此文章:Add-ins and Extensibility。
Base
的具体类的实例,并将它们存储在列表中。
下面的代码片段(C#)构造了一个由派生自 Base
的任何具体类组成的实例列表,这些类可以在应用程序路径中的类库(*.dll)中找到。
using System.IO;
using System.Reflection;
List<Base> objects = new List<Base>();
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
foreach (FileInfo file in dir.GetFiles("*.dll"))
{
Assembly assembly = Assembly.LoadFrom(file.FullName);
foreach (Type type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(Base)) && type.IsAbstract == false)
{
Base b = type.InvokeMember(null,
BindingFlags.CreateInstance,
null, null, null) as Base;
objects.Add(b);
}
}
}
编辑: 在.NET 3.5中,Matt提到的类可能是更好的选择。
关于如何动态加载.NET程序集的信息,请参见这个问题(以及我的答案)。以下是创建一个AppDomain
并将程序集加载到其中的代码。
var domain = AppDomain.CreateDomain("NewDomainName");
var pathToDll = @"C:\myDll.dll";
var t = typeof(TypeIWantToLoad);
var runnable = domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName)
as IRunnable;
if (runnable == null) throw new Exception("broke");
runnable.Run();
插件框架的一个典型需求是卸载插件。要卸载动态加载的程序集(例如插件和附加组件),必须卸载包含它们的AppDomain
。有关详细信息,请参见MSDN上有关Unloading AppDomains
的本文。
有一个stackoverflow问题和答案描述了如何使用Windows Communication Framework(WCF)创建插件框架。
我知道两个插件框架:
System.AddIn
命名空间,如Matt在他的答案中提到的。有些人将Managed Extensibility Framework (MEF)视为插件或附加组件框架,但它并不是。有关详细信息,请参见此StackOverflow.com问题和此StackOverflow.com问题。
一个提示是将所有插件和类库加载到自己的AppDomain中,因为运行的代码可能是恶意的。自己的AppDomain也可以用于“过滤”你不想加载的程序集和类型。
AppDomain domain = AppDomain.CreateDomain("tempDomain");
要将程序集加载到应用程序域中:
AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
Assembly assembly = domain.Load(assemblyName);
AppDomain.Unload(domain);
[DllImport("kernel32.dll")]
internal static extern IntPtr LoadLibrary(String dllname);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
public interface IYourInterface
{
Task YourMethod();
}
public class YourClass: IYourInterface
{
async Task IYourInterface.YourMethod()
{
//.....
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Linq;
public abstract class ReflectionTool<TSource> where TSource : class
{
public static TSource LoadInstanceFromLibrary(string libraryPath)
{
TSource pluginclass = null;
if (!System.IO.File.Exists(libraryPath))
throw new Exception($"Library '{libraryPath}' not found");
else
{
Assembly.LoadFrom(libraryPath);
var fileName = System.IO.Path.GetFileName(libraryPath).Replace(".dll", "");
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(c => c.FullName.StartsWith(fileName));
var type = assembly.GetTypes().FirstOrDefault(c => c.GetInterface(typeof(TSource).FullName) != null);
try
{
pluginclass = Activator.CreateInstance(type) as TSource;
}
catch (Exception ex)
{
LogError("", ex);
throw;
}
}
return pluginclass;
}
}
然后像这样调用它:
IYourInterface instance = ReflectionTool<IYourInterface>.LoadInstanceFromLibrary("c:\pathToYourLibrary.dll");