0

I get all classes that inherit from my abstract base class like this:

Assembly myAssembly = Assembly.GetExecutingAssembly();
var classesThatInheritFromBase = myAssembly.GetTypes().Where(x => x.BaseType ==  
typeof(BaseClass));

Now can I somehow get all/any instances of all these classes? Does this still belong to reflection?

Cencek
  • 33
  • 5

1 Answers1

0

It would be the easiest if you made all your singletons implement an interface, say ISingleton, with a single method returning the instance, let's call it GetInstance. Then you could get all the types that have ISingleton in their GetInterfaces() enumerable, catch the GetInstance method by reflection and invoke it.

There is no way to simply find all instances of a given type, as you would have to somehow walk through the entire allocated heap space and stack space of all running threads, which is... hard to achieve to say the least.

V0ldek
  • 9,623
  • 1
  • 26
  • 57