简体   繁体   中英

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:

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.

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.

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. You create two classes, your useful class, and a class factory class. Your class factory class implements 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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