Determine class type in static method

E

Erik Reischl

Hello,

I have a static method in a class which also has another class inherited
from.

Now, I'd like to determine the type of class from where the method call was
made.

Consider the following code fragment:

~~~
using System;
using System.Reflection;
using System.Diagnostics;

namespace Dummy
{
class Startup
{
static void Main()
{
ClassB.DoSomething();
}
}

class ClassA
{
public static void DoSomething()
{
// How can I determine HERE from which subclass (here:
ClassB)
// the call was made???
Type t = ... // ?
}
}

class ClassB : ClassA
{
}
}
~~~

So in "DoSomething", I'd like to get a Type t which would be ClassA, if I
write ClassA.DoSomething() and in the above case should be ClassB, as it is
ClassB.DoSomething().

The problem is that the method is static, so there is no "this".

I have done my "homework" and searching the net and this and other
newsgroups on this topic. On some cases, it was advised to use the call
stack (System.Diagnostics.StackTrace). However, this leads me to ClassA in
every case.

I also read quite a few posts calling for a re-design, as every subclass
should implement DoSomething() on its own. Theoretically I agree. In my
special case, DoSomething is sort of an object factory for objects of
subclasses. I write "sort of", as I know that the factory design pattern is
quite different from this approach. If you have ever seen ECO (Enterprise
Core Objects), you might know what I mean.

The only solution have found so far is to give the type of the subclass to
DoSomething as a parameter:

ClassA.DoSomething(typeof(ClassB))

which is of course identical to

ClassB.DoSomething(typeof(ClassB))

So if there is no other way, I'll stick to that. Otherwise, I'd be happy to
know how DoSomething could determine the type it was called from.

Best wishes,

Erik
 
J

Jon Skeet [C# MVP]

I have done my "homework" and searching the net and this and other
newsgroups on this topic. On some cases, it was advised to use the call
stack (System.Diagnostics.StackTrace). However, this leads me to ClassA in
every case.

Indeed it would. Compile your code and then use ildasm to see what it's
actually compiled to - there's no trace of a call to
ClassB.DoSomething, and indeed if you put a static constructor in
ClassB you'll see it's never initialized.

The only solution have found so far is to give the type of the subclass to
DoSomething as a parameter:

ClassA.DoSomething(typeof(ClassB))

which is of course identical to

ClassB.DoSomething(typeof(ClassB))

So if there is no other way, I'll stick to that. Otherwise, I'd be happy to
know how DoSomething could determine the type it was called from.

Short of the redesign (for which I'd need to know more context, but I
suspect it would be the best way to go - create a separate class
hierarchy for the factories, and use instance methods rather than
static methods) the parameter approach is the only one which will work.
 
I

Ignacio Machin ( .NET/ C# MVP )

The problem is that the method is static, so there is no "this".

I have done my "homework" and searching the net and this and other
newsgroups on this topic. On some cases, it was advised to use the call
stack (System.Diagnostics.StackTrace). However, this leads me to ClassA in
every case.

That is weird, of course, the first frame in the stack is ClassA, but
if you go down further you should see who called it.
 

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

Top