Use reflection to determine subtype

  • Thread starter Thread starter Derrick Morin
  • Start date Start date
D

Derrick Morin

I can use the following to determine the Type of the current instance.

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

What I want is the Type of the sub class instance. Any ideas? Here's an
example.

namespace myNamespace
{
public class A
{
protected Type myType =
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
protected Type myChildType = ????
}

public class B : A
{
}
}

When I create and instance of B, the myType member is a Type with name
"myNamespace.A". I want the myChildType member to be the Type with name
"myNamespace.B". Any ideas?

I DO NOT want to modify class B.

Thanks
Derrick
 
Derrick Morin said:
I can use the following to determine the Type of the current instance.

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

Nope, that declares the type that declares the current method.

Just use:

Type t = this.GetType();

(the "this" is unnecessary but you might find it helps readability.)
 
Back
Top