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
Jon Skeet [C# MVP] - 27 Mar 2008 12:56 GMT
<snip>
> 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.
<snip>
> The only solution have found so far is to give the type of the subclass to
> DoSomething as a parameter:
[quoted text clipped - 7 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Ignacio Machin ( .NET/ C# MVP ) - 27 Mar 2008 15:53 GMT
> 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.