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
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