[Gambas-user] Enumerate all classes inheriting X

Tobias Boege taboege at ...626...
Sat Nov 7 15:29:28 CET 2015


On Sat, 07 Nov 2015, Beno??t Minisini wrote:
> Le 06/11/2015 06:04, Tobias Boege a ?crit :
> > Hi,
> >
> > in my project I have the need to enumerate all classes from my current
> > project (not from components) for which
> >
> >    hClass Is X
> >
> > is true for some fixed class X, once at startup. I think I tried
> >
> >    For Each hClass In Classes
> >      hCurrent = hClass
> >      While hCurrent.Parent
> >        If hCurrent.Parent = Classes["X"] Then
> >          aRes.Add(hClass)
> >          Break
> >        Endif
> >        hCurrent = hCurrent.Parent
> >      Wend
> >    Next
> >
> > (written from memory, but it gets the idea across). However, this does not
> > work as Classes can only access the already loaded classes.
> >
> > Is there a way to enumerate _all_ classes in the current project or to force
> > loading of all remaining classes from the project? I have no particular
> > technical problem with hard-coding the desired list instead of computing it
> > but I want to know if I *could* compute it.
> >
> > Regards,
> > Tobi
> >
> 
> At the moment, you can't get any information on a class without loading 
> it into the interpreterer, which may have unwanted effects...
> 
> I have the same need, so I may provide a solution in the future!
> 

Cool. In the meantime, to close this thread, I have decided for my project
to list the relevant classes explicitly.

It was about testing for all sub-classes of a specific class whether they
could be constructed from a certain argument (i.e. if their constructors
don't throw an error). Since I control all classes in the project, I found
it less obscure and painful to write

  Dim hObj As MyClass

  Try hObj = New MySubClass1(Arg)
  If Not Error Then Return hObj
  Try hObj = New MySubClass2(Arg)
  If Not Error Then Return hObj
  ...
  Error.Raise(("No compatible implementation found"))

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list