static abstract methods

  • Thread starter Thread starter Darren
  • Start date Start date
Darren said:
How would you force a derived class to implement a static method?

You can't - and even if you could, there'd be no way of polymorphically
calling it.
 
In my case I don't need to use it polymorphically I just need to enforce an
implementation of a static method.
I've read through some of the previous posts and they all seem to degenerate
from the problem at hand.
 
Darren said:
In my case I don't need to use it polymorphically I just need to enforce an
implementation of a static method.

I'd do it with unit tests, to be honest - it's the most pragmatic way
of checking, given the language.
I've read through some of the previous posts and they all seem to degenerate
from the problem at hand.

I think it's a genuine concern, along with enforcing specific
constructors, but C# and .NET definitely don't have any way of doing it
at the moment. It's *usually* better designed around (with factories or
whatever) but there are a few cases where it's a genuine problem :(
 
You can enforce it at run time using reflection. At least you'll find
the problem pretty quickly when you start testing. You can make the
"check()" method below only compile for DEBUG.

To get a little better compile time errors, you may be able to put an
abstract method
abstract protected MethodInfo GetRequiredStaticMethod();

Then derived classes will at least know at compile time that they must
implement this method and will hopefully figure out that they should
implement the static method that it is supposed to return.

this is how to check for the method from the base class. make sure
check() is called from every constructor:
------
using System;
using System.Reflection;
using System.Diagnostics;

namespace abstractstatictest
{

class asbase
{
// require
// static public int asmethod(int param)

public static bool check(asbase a)
{

foreach(MethodInfo mi in a.GetType().GetMethods())
{
Debug.WriteLine(mi.Name);

Type returnType = mi.ReturnType;
Type expectedReturnType = Type.GetType("System.Int32");


if (mi.IsStatic &&
returnType==expectedReturnType &&
mi.GetParameters().Length == 1)
{
Type paramType = mi.GetParameters()[0].ParameterType;
Type expectedParamType = Type.GetType("System.Int32");

if (paramType == expectedParamType)
{
return true;
}
}
}
throw new ApplicationException("Class " + a.GetType().Name + " does
not implement 'static int public asmethod(int param);");
}

public asbase()
{
check(this);
}
}

class foo : asbase
{
static public int asmethod(int param1) { return 0;}

public foo() { }
}

class bar : asbase
{

public bar() { }
}


public class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
foo f = new foo();
}
catch(Exception x)
{
Debug.WriteLine(x.Message);
}

try
{
bar b = new bar();
}
catch(Exception x)
{
Debug.WriteLine(x.Message);
}
}
}
}
 
....
I think it's a genuine concern, along with enforcing specific
constructors, but C# and .NET definitely don't have any way of doing it
at the moment. It's *usually* better designed around (with factories or
whatever) but there are a few cases where it's a genuine problem :(

Jon,

Do you have an example handy? I'd like to see it. I keep seeing people
asking about how to do that, but never saw an interesting real world
example.

Regards,
Alexander
 
Alexander Shirshov said:
...

Do you have an example handy? I'd like to see it. I keep seeing people
asking about how to do that, but never saw an interesting real world
example.

Well, it may well not solve the problem you want, but if you want to
have a class which has a way of returning a new instance of the class,
given specific parameters, then rather than relying on a static method
you could use an abstract base class or an interface:

public interface IFoo
{
....
}

public interface IFooFactory
{
Foo CreateFoo(string name);
}

This is sometimes used in plugin development, but then you still have
the issue of how an instance of the class implementing IFooFactory is
created in the first place.

Could you tell me more about the problem you're trying to solve? That
way I may be able to give a more concrete solution.
 
David Lei said:
You can use Interface to force all classes to have the same set of methods.

Only instance methods though - not static methods, as the OP asked
about.
 
Back
Top