Code Monkey home page Code Monkey logo

plugincore's Introduction

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.