inheriting with static methods and type passing, recommendation?




I have a baseclass (non-static) with some static and some non-static

In the baseclass in one of the static methods I need to do something like

The problem is, that I need to be able to change CallToStaticMethod() in the
derived class and call this instead.
I do not want to copy / paste
into the derived class.

Since it is a static, I cannot use on "CallToStaticMethod"
"virtual/override" or "abstract/override", if I use "new" I hide the
"CallToStaticMethod", however, calls in the base-class of course go to the
original CallToStaticMethod

So my workaround for now was to pass the type of the derived class to the

and in the base class implement something like:
static void StaticMethodOnlyInBaseClass(Type callingType)
callingType.GetMethod("CallToStaticMethod").Invoke(null, null);

This is quite clumsy...
can anyone think of another way?
Would c# generics help me here?

(Please do not forget that I need to have both, non-static and static members)

If so, how?




Bruce Wood

Why does the method need to be static? Maybe it's just my foggy brain,
but I can't think of any reason why a _method_ would _have_ to be
static in this context, unless it's being invoked in other situations.
New, static _state_, that's a whole other can of worms. So, I see two
possible solutions, depending upon your situation:

1. You're struggling to make a static method participate in inheritance
when the static method is used only in this context. Solution: make the
static method a virtual instance method that manipulates static data.

2. You need the static method because it's called in other contexts as
a static method, so it absolutely can't be an instance method.
Solution: write a virtual instance method that does nothing but call
the static method. Override it in derived classes to call other static
methods in those classes.

Nicholas Paldino [.NET/C# MVP]


The only way you can do this would be to actually implement it as an
instance method which is virtual, and then call the virtual method. It's a
small cost to pay for the type safety you would have.



Think of it something similar like

Actually it is a little more difficult than that, since there is an actual
constructor, the static methods in this case do something else, however in
the way of processing they need to actually create an instance object of the
same type and do something with it (e.g. write a Serialization of a default
instance object somewhere).

In my case, the base-class does quite some things in the equivalent to the
However, one of the other static methods called inside the base classes
"Create" I need to change in order to provide different functionality in the

So, I cannot really create an instance of the derived class in the base
class, since the base-class does not know much about the derived class.

I checked the mechanisms I could use with constructors, however those do not
seem to help me here either (I would have to repeat too much of the code in
the derived class again).


What do you think about that:

public class ClassB<T> where T : ClassA,new()
private static T myField = new T();

public static void MyStaticMethod()

public class ClassA
public virtual void Method()
//do "static" work

If you need some new functionality you can change generic parameter or inherit from generic class

public class ClassC : ClassA
public void override Method()
//do new "static" work

public ClassD : ClassB<ClassC>



ClassD cInstance = new ClassD();



Bruce Wood

However, one of the other static methods called inside the base classes "Create" I need to change in order to provide different functionality in the derived-class.

First, I feel compelled to point out that, at least in C#, this
statement doesn't make much sense. The base class's "Create" can't
provide "different functionality in the derived class". Static methods
are completely stand-alone and do not participate in inheritance, so a
base class static method is simply that: of the base class, and has
nothing to do with the derived class at all except where variable
scoping and permissions are concerned. By the same token, a derived
class static method is related to the base class only where permissions
are concerned. Just because the two methods have the same name doesn't
make one somehow an "override" of the other.

That said, I think I see what you're driving at. Do you mean this:

"I have a base class with a static method, let's call it
'StaticMethod'. The derived classes of this base class will also have
static methods (with the same name, but that's not really an important
aspect of the problem) that will do almost the same thing, but with
slight variations in behavior. I don't want to duplicate all of the
code for the base class's static method in all derived classes, but
neither can I simply call the base class's static method from derived
classes, because the behavior difference is buried in the middle of the
method. How do I write the derived class's static methods with a
minimum duplication of code?"

If that is indeed your question, then you have to handle it like a
procedural programming problem, because you are, in effect, back in the
world of procedural programming: static means no inheritance, which
means, effectively, no O-O. Back in my old procedural days, we used to
handle this by breaking up said static method into smaller methods,
implementing those in the "base class", and then having the "derived
class" methods use the same parts, viz:

public static void StaticMethod()

and then, in the derived classes:

public void static StaticMethod()
// Skip the other thing the base class did here

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question