A lite plugin framework on .NET(.NET Core), which can be used to build plug-in programs.
.NET 7 .NET 6 .NET Core 3.1
You need create at least 3 projects.
dotnet new classlib -o MyApp.Interface
dotnet new classlib -o MyPlugin
dotnet new console -o MyApp
Define the interface of the plugin:
IPlugin.cs
public interface IPlugin
{
public string MakeText();
}
Write code for the first plugin:
MyPlugin.cs
public class MyPlugin : IPlugin
{
public string MakeText() => "Hello, I'm MyPlugin!";
}
MyPlugin.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!--
Add option here.
See https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support
-->
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../MyApp.Interface/MyApp.Interface.csproj">
<!-- And here. -->
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>
</Project>
Build it:
dotnet build
Then, put the myplugin.dll
into the base directory of the main program.
dotnet add package Nefe.PluginCore
Program.cs
using Nefe.PluginCore;
var plugin = new Plugin(Path.Combine(AppContext.BaseDirectory, "myplugin.dll"));
if (plugin.LoadFromAssemblyPath() is Assembly assembly)
foreach (var instance in plugin.CreateInstancesFromAssembly<IPlugin>(assembly))
Console.WriteLine(instance.MakeText());
Build & Run it:
dotnet run
If all goes well, you'll see:
Hello, I'm MyPlugin!