Find out most derived class in base class
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 22 08:21:41 PST 2010
On Fri, 19 Nov 2010 16:05:40 -0500, Jonathan M Davis <jmdavisProg at gmx.com>
wrote:
> On Friday 19 November 2010 12:56:38 Steven Schveighoffer wrote:
>> On Fri, 19 Nov 2010 15:54:08 -0500, Michal Minich
>>
>> <michal.minich at gmail.com> wrote:
>> > On Fri, 19 Nov 2010 20:50:13 +0000, Michal Minich wrote:
>> >> How do I solve this, without parametrizing class.
>> >>
>> >> class Base {
>> >>
>> >> void foo () { I want get somehow to type *Derived2* at CT }
>> >>
>> >> }
>> >>
>> >> class Derived1 : Base { }
>> >> class Derived2 : Base { }
>> >
>> > By making mistake, I realized that it is not possible :) The last line
>> > should be:
>> >
>> > class Derived2 : Derived1 { }
>> >
>> > The *Base* has two most derived classes in the first example.
>> >
>> > Anyway, is it somehow possible to iterate derived classes if I only
>> know
>> > base class? At CT.
>>
>> No. You don't know all the derived classes until link time, and there's
>> no doing anything except linking at link time.
>>
>> You should be able to find out at runtime which classes are derived from
>> Base.
>
> And how would you know that at runtime? All reflection in D at this
> point is
> compile-time reflection, and that isn't going to help you any here (for
> the very
> reasons that you list).
All the ModuleInfo and ClassInfo types are stored in an array somewhere in
the runtime. Take a look at Object.factory() for an example of how to
find a class.
If you can iterate all the ClassInfo's in the application, you can find
out which ones derive from a base.
-Steve
More information about the Digitalmars-d-learn
mailing list