简体   繁体   English

如何强制子类实现静态方法

[英]How to force sub class to implement a static method

I understand this is only possible with a workaround .我知道这只能通过一种解决方法来实现 But why?但为什么?

I want to add plugin support to my app.我想为我的应用程序添加插件支持。 So I designed an abstract class that all future plugins will need to implement.所以我设计了一个抽象类,所有未来的插件都需要实现。 Every plugin must implement a GetVersion() method like in this example code:每个插件都必须实现一个GetVersion()方法,如本示例代码所示:

public abstract class Plugin
{
    public abstract int GetVersion();
}

public class MyPlugin : Plugin
{
    public override int GetVersion()
    {
        return 1;
    }
}

This of course works perfectly as long as I instantiate the plugin before calling the GetVersion() method.只要我在调用GetVersion()方法之前实例化插件,这当然可以完美运行。

But if I want to get the version number of the plugin before creating an instance of it?但是如果我想创建它的实例之前获取插件的版本号? To check compatibility for example?例如检查兼容性?

public class Program
{
    public Program()
    {
        if (MyPlugin.GetVersion() > 1)
        {
            PluginLoader.Load(new MyPlugin());
        }
    }
}

Although it might not answer directly your question "WHY" I think below solution might be usefull in your scenario:虽然它可能不会直接回答您的问题“为什么”,但我认为以下解决方案可能对您的场景有用:

Use assembly version attribute :使用程序集版本属性

  Assembly thisAssem = typeof(MyPlugin).Assembly;
  AssemblyName thisAssemName = thisAssem.GetName();

  Version ver = thisAssemName.Version;

It never can be done by C# because a static method cannot be implemented in derived classes.它永远不能由 C# 完成,因为不能在派生类中实现静态方法。

Like the workaround, you can create a static factory to create the instance.与解决方法一样,您可以创建一个静态工厂来创建实例。

public abstract class Plugin
{
    public abstract int GetVersion();
}

public class FactoryPlugin<T> where T : Plugin, new()
{
    public static int GetVersion()
    {
        return new T().GetVersion();
    }
}

public class Program
{
    public Program()
    {
        if (FactoryPlugin<MyPlugin>.GetVersion() > 1)
        {

        }
    }
}

Consider using the Factory pattern in a way similar to what a COM class factory does.考虑以类似于 COM 类工厂的方式使用工厂模式。 You create two classes, your useful class, and a class factory class.您创建了两个类,您的有用类和一个类工厂类。 Your class factory class implements IPluginFactory.您的类工厂类实现了 IPluginFactory。 You package it with your Plugin.你用你的插件打包它。 The plugin factory has vary simple methods, but one of them allows your Plugin to be created.插件工厂有各种简单的方法,但其中之一允许创建您的插件。 It's close to what @ThierryV showed, but without static methods.它与@ThierryV 显示的很接近,但没有静态方法。 So the process is:所以过程是:

  • Use whatever you are planning to use to store and instantiate your plugins, but instead of instantiating a plugin, you instantiate the appropriate Plugin Factory使用您计划用来存储和实例化插件的任何东西,但不是实例化插件,而是实例化适当的插件工厂
  • You can have the Plugin factory do what ever you want -- get detailed information about the plugin, allow instantiation of the latest version or a particular version of the plugin - go to town你可以让插件工厂做任何你想做的事情——获取关于插件的详细信息,允许实例化最新版本或特定版本的插件——去镇上
  • But, eventually, you use an instance of the factory to instantiate your Plugin.但是,最终,您使用工厂的实例来实例化您的插件。

This is a good place to start: What exactly is a Class Factory?这是一个很好的起点: 什么是类工厂? , but Don Box's Essential COM book is where I learned all this stuff, a long time ago in a place far away. ,但是 Don Box 的 Essential COM 书是我很久以前在遥远的地方学到所有这些东西的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM