Skip to content

ReturnNefe/PluginCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Nefe.PluginCore

A lite plugin framework on .NET(.NET Core), which can be used to build plug-in programs.

Requirements

.NET 7 .NET 6 .NET Core 3.1

QuickStart

You need create at least 3 projects.

dotnet new classlib -o MyApp.Interface
dotnet new classlib -o MyPlugin
dotnet new console -o MyApp

MyApp.Interface Project

Define the interface of the plugin:

IPlugin.cs

public interface IPlugin
{
    public string MakeText();
}

MyPlugin Project

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.

MyApp Project

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!

About

A Plugin Framework on .NET Core

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages