#Getting All Instances Of Class

18 messages · Page 1 of 1 (latest)

sage lance
#

Sorry, I'm back!
Is it possible to get all instances of a given class during runtime (probably using reflection, preferably without any extra dependency).
Thanks again!

little trellisBOT
#

This post has been reserved for your question.

Hey @sage lance! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tame dune
#

How many instances are you expecting?

#

Since if you're using expecting one instance, just use a singleton. Otherwise things might get trickier

odd goblet
#

I've personally used it for reflecting and it is pretty easy and efficient to use

sage lance
odd goblet
#

Oh nice! What are you trying to do by reflecting all the class instances?

#

May give some more insight as to how we can help

strange spire
#

If it's a class that you yourself have made, I would recommend avoiding reflection and instead implementing a registry to record the instances you're creating. Create a static factory method to return all of the instances you're creating, and at the same time, store weak references to those instances in a cache.

#

From a quick google, it doesn't seem like there's a well known way to get all active instances of an arbitrary class during normal runtime.

tame dune
#

It wouldn't be thread safe anyways, so custom implementation is really the only way.

sage lance
#

Anyway, thanks for trying. Gonna have to find another way :/

little trellisBOT
strange spire
#

Good luck. Gimme a ping if you find any interesting solutions.

sage lance
#

k thx