Search code examples
c#staticvirtualabstract

C# call abstract method from static method


i am writing library, that can be used for with different database engines. Abstract class has abstract DbParameter constructor and uses classes from System.Data.Common.

Now i have sutch structure :

public abstract class ASqlWorker{

 abstract protected DbParameter DbParameterConstructor(String paramName, Object paramValue);

}

now, can i call abstract DbParameterConstructor from a static method of ASqlWorker?

Logicaly, i could make abstractMethod static (it creates instance of an inheriter of DbParameter from name and value and doesn't use fields of ASqlWorker), but it is not allowed in C#. btw, it is not allowed because of usage of non-realised static method can cause troubles. But in my case it won't, because my abstract method is protected.

i just want to write some implicit convertor from DbParameter to new DbParameter[1] to have more flexible interfaces.


Solution

  • I did it!

    lol, i implemented abstract static method in C# !

    what did i:

    • created abstract class AbstractDbParameterConstructors, that don't contains any state, only abstract methods.
    • class ASqlWorker now gets generic, inhereted from AbstractDbParameterConstructors.

      public abstract partial class ASqlWorker<TPC>
                  where TPC : AbstractDbParameterConstructors, new()
      { ... }
      
    • declared private static variable

      private static TPC generator = new TPC();
      

    here both i have something like abstract static method, and i am protected from undesirable effects, from witch creators of standard were trying to protect me.

    deatails : http://sourceforge.net/projects/sqlworker/