Find the name of the derived class in the base classmethod/constructor

P

puzzlecracker

Hello,

That's pretty crazy requirement -- I would like to find out whether I
can find a name of derived class in the base class method/ctor for
reflection purposes.

public abstract class Base
{
void Foo(){ string derivedClassName=/* Name of the class
called via it's instance*/; }

}

Public class DerivedA:Base
{


}

Public class DerivedB: Base
{


}

void Test()
{
DerivedB d=new DerivedB();
Base d2=new DerivedB();
d.Foo(); // I want derivedClassName to best set to DerivedB
}

Same way for constructor -- To know which derived class called base
class constructor.

Is it even possible? What's the workaround, as I am sure it's a fairly
common problem in a framework/Library design.


Thanks
 
P

puzzlecracker

I have just discovered something interesting, and vastly different
from other programming languages notably c++ and java, that csharp
gives you the name of the type before the instance of that type is
fully constructed. I am using vs2008. Let me illustrate with an
example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
public class Base
{
public Base()
{
System.Console.WriteLine("In Base::Base() " + GetType
().Name);
}
public void Foo()
{
System.Console.WriteLine("In Base:Foo() " + GetType
().Name);

}

}
public class Derived:Base
{

}
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Foo();

return;
}
}
}


It outputs:
In Base::Base() Derived
In Base::Foo() Derived


I think, that first line should be "In Base::Base() Base to underline
the fact that at that point the object is really of a Base type, much
like it's done in CPP.
 
J

Jeroen Mostert

puzzlecracker said:
I have just discovered something interesting, and vastly different
from other programming languages notably c++ and java, that csharp
gives you the name of the type before the instance of that type is
fully constructed. I am using vs2008. Let me illustrate with an
example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
public class Base
{
public Base()
{
System.Console.WriteLine("In Base::Base() " + GetType
().Name);
}
public void Foo()
{
System.Console.WriteLine("In Base:Foo() " + GetType
().Name);

}

}
public class Derived:Base
{

}
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Foo();

return;
}
}
}


It outputs:
In Base::Base() Derived
In Base::Foo() Derived


I think, that first line should be "In Base::Base() Base to underline
the fact that at that point the object is really of a Base type, much
like it's done in CPP.
But the object *isn't* of Base type at that point, or indeed ever. C# makes
a lot of sense here; objects do not change type at runtime, so it's only
logical that the return value of GetType() is always the same. How the
object is *constructed* is another matter, but just because the
initialization of the derived members isn't yet complete doesn't somehow
make the object have a different type.

C# isn't comparable to C++ in this regard if only because C# ensures that
memory and object meta-information have been initialized prior to calling
the constructor, while doing stuff like dynamic type identification in a C++
constructor is a perilous affair.
 
P

Pavel Minaev

I have just discovered something interesting, and vastly different
from other programming languages notably c++ and java, that csharp
gives you the name of the type before the instance of that type is
fully constructed.

Actually, Java does the same thing, and so do pretty much all other
languages out there for which the change is actually detectable. The
only language I know that has this curious "object type changes during
construction and destruction" semantics is C++.

And yes, it is a conscious design decision by the language designers,
and not an oversight.
 

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