Getting type of inheriting class from static methods in base class

  • Thread starter /kim/birkelund/aka/sekhmet
  • Start date
K

/kim/birkelund/aka/sekhmet

Hi

I have an abstract base class from which I would be able to reflect on the
members declared by an inheriting type.

Fx. if I have a class CBase which is a an abstract class with a static
constructor and I have a class C that extends CBase.
If I do something like:
typeof(C).GetProperty(...).GetValue(...)

It of course calls my static constructor in CBase. What I would like to be
able to is from that static constructor to get the members defined in C
(i.e. not only the abstract members declared in CBase).

If this is done from an instance member there's no problem, I just class
this.GetType() and I get the Type object for my inherited type. So what I'm
actually looking for is a static equivalent to this.GetType().

Hope someone can help me (and that I'm posting in the right group :))


/kim/birkelund/aka/sekhmet
 
J

Jon Skeet [C# MVP]

/kim/birkelund/aka/sekhmet said:
I have an abstract base class from which I would be able to reflect on the
members declared by an inheriting type.

Fx. if I have a class CBase which is a an abstract class with a static
constructor and I have a class C that extends CBase.
If I do something like:
typeof(C).GetProperty(...).GetValue(...)

It of course calls my static constructor in CBase. What I would like to be
able to is from that static constructor to get the members defined in C
(i.e. not only the abstract members declared in CBase).

If this is done from an instance member there's no problem, I just class
this.GetType() and I get the Type object for my inherited type. So what I'm
actually looking for is a static equivalent to this.GetType().

Hope someone can help me (and that I'm posting in the right group :))

typeof(C) is really the equivalent here. It's not nice for cutting and
pasting, but it's the only thing which is actually accurate.
 
K

/kim/birkelund/aka/sekhmet

I can't really have typeof(C) in a member of the base class CBase, since
that would ruin the whole idea of inheriting.

I could of course do something like having a property in the base class
designed to hold the type of the inheriting class and set that from the
inheriting class' static constructor. But that would be really ugly, and
impossible to verify at compile time.

Is that really my only option?


/kim/birkelund/aka/sekhmet
 
K

/kim/birkelund/aka/sekhmet

Just realised that actually I can't do that either, since the static
constructor for the base class is invoked before the one in the inheriting
class. Bummer.



/kim/birkelund/aka/sekhmet said:
I can't really have typeof(C) in a member of the base class CBase, since
that would ruin the whole idea of inheriting.

I could of course do something like having a property in the base class
designed to hold the type of the inheriting class and set that from the
inheriting class' static constructor. But that would be really ugly, and
impossible to verify at compile time.

Is that really my only option?


/kim/birkelund/aka/sekhmet
 
J

Jon Skeet [C# MVP]

/kim/birkelund/aka/sekhmet said:
I can't really have typeof(C) in a member of the base class CBase, since
that would ruin the whole idea of inheriting.

I could of course do something like having a property in the base class
designed to hold the type of the inheriting class and set that from the
inheriting class' static constructor. But that would be really ugly, and
impossible to verify at compile time.

Is that really my only option?

Yes. Bear in mind that if you call a static method (or property) using
a derived class, only a reference to the base class is emitted by the
compiler. For instance:

Encoding u = UnicodeEncoding.Unicode;

gets compiled to the *exact* same code as

Encoding u = Encoding.Unicode;

There's no concept within the Unicode property of the Encoding class
about whether it was called "through" UnicodeEncoding, ASCIIEncoding,
Encoding, or anything else.
 

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