0

I want to override a static method from a DLL Export

public class Export {
[DllExport] public static string plugin_name() { return Plugin.Instance.plugin_name(); }
}
    public class Plugin<T> where T: Plugin<T>, new()
    {
        private static readonly Lazy<T> val = new Lazy<T>(() => new T());
        public static T Instance { get { return val.Value; } }

        protected Plugin() { }

        public new static string plugin_name() { }
    }
}

so these classes are in a dll file now I want that people who use the dll only do that in the main class.

public class Main : Plugin<Main> {
   public override string plugin_name() { 
       return "a test plugin";
   }
}

I have tested it for hours but failed.

Hanrs Porj
  • 19
  • 2
  • Possible duplicate of [Can a static method be overridden in C#?](https://stackoverflow.com/questions/14828271/can-a-static-method-be-overridden-in-c) – Mark Baijens Jun 07 '19 at 11:55
  • 1
    You can't make any demands whatsoever on [DllExport] code. That class Main is certainly not the way, say, a C programmer would use it. – Hans Passant Jun 07 '19 at 13:38

1 Answers1

0

You can not override static methods. You need to make a virtual or abstract instance method.

public abstract class Plugin<T> where T : new()
{
    private static readonly Lazy<T> val = new Lazy<T>(() => new T());
    public static T Instance { get { return val.Value; } }

    protected Plugin() { }

    public abstract string plugin_name();
}

public class Main : Plugin<Main> {
    public override string plugin_name() => "a test plugin";
}

To make the method plugin_name static also does not make much sense, since you anyway create a singleton instance.

You can check out the code here.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
Sefe
  • 13,731
  • 5
  • 42
  • 55
  • Thanxs for your answer, I have forgot that I need to return the `plugin_name` in the DllExport `return Plugin.Instance.plugin_name(); ` the problem is that I need the type from Plugin but the Main class is not in the dll file how can I make it? – Hanrs Porj Jun 07 '19 at 12:32
  • why I use the singleton because I need some more variables in Plugin for other classes e.g `Plugin.Instance.ApiVersion` but the problem is that I dont know how to create a instance without that I only nee to make `Plugin.Instance.plugin_name()` that would fix all problems – Hanrs Porj Jun 07 '19 at 12:42