Skip to content

API Loader Plugins

3cxc edited this page Sep 12, 2026 · 2 revisions

NativePluginKit.Loader.Plugins

Plugin base class and runtime context.

Type Description
Plugin 所有插件的基类
PluginContext 提供插件运行时的全局上下文

Plugin

public abstract class Plugin

所有插件的基类,提供与宿主程序交互的基础功能。插件继承此类并实现 OnStart 和 OnStop 方法。

Properties

Member Type Description
Api HostApiTable 获取宿主程序提供的 API 函数表
Name string 插件名称
Description string 插件描述信息
Author string 插件作者
Version Version? 插件版本号,默认为所在程序集的版本号
RequiredApiVersion Version 插件所要求的宿主程序 API 版本

Methods

OnStop()

public abstract void OnStop()

插件停止时调用的抽象方法,子类必须实现具体的清理逻辑。

Note on OnStart() — OnStart is protected abstract and is called automatically after the plugin is initialized. It is intentionally hidden from the public API surface.

Source-Generated Members

The following members are generated by the NativePluginKit source generator and are not part of the base class:

Member Description
__GeneratedOnInit(IntPtr) Exported as OnInit. Registers metadata and constructs the plugin instance.
__GeneratedOnStop() Exported as OnStop. Invokes the instance's OnStop and clears the instance reference.
__GeneratedModuleInit() [ModuleInitializer] that calls SetPluginInfo(new YourPlugin()) on assembly load.

The plugin class must be declared partial for the source generator to inject these members.

Example

public partial class HelloWorldPlugin : Plugin
{
    public override string Name               => "HelloWorldPlugin";
    public override string Description        => "A simple hello world plugin.";
    public override string Author             => "Your Name";
    public override Version RequiredApiVersion => new(1, 0, 0);

    protected override void OnStart()
    {
        Features.Logger.Log.PrintLog("Hello, World!");
    }

    public override void OnStop() { }
}

PluginContext

public static class PluginContext

提供插件运行时的全局上下文,用于访问宿主 API 函数表。

Properties

Member Type Description
CurrentApi HostApiTable 获取当前插件使用的宿主 API 函数表。该属性在插件初始化时被设置。

Clone this wiki locally