0

It's possible to obtain a collection of all objects of certain class?

Given some arbitrary class, for example:

public class MyClass {
   public int anyVal;
   /* ...etc ... */
}

Is it possible to obtain a collection of all objects of certain class so you can iterate through them, like perhaps:

MyClass obj = new MyClass();
obj.anyVal = 333;

MyClass[] allObjects = //???              // <== HERE

foreach (MyClass mObj in allObjects) {
    Console.WriteLine("obj = " + mObj.anyVal);
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
XXX YYY
  • 3
  • 5

1 Answers1

0

No. You'd have to examine the Heap somehow and check the type for every object there. Not something you want to be doing.

Rik
  • 28,507
  • 14
  • 48
  • 67