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);
}
8
  • Possible duplicate of C# Iterate through Class properties Commented Mar 4, 2016 at 12:11
  • @fubo i think the question is about instances of a class, but not about encapsulated objects. Commented Mar 4, 2016 at 12:12
  • Do you mean iterating over all the properties? It's really unclear what you're trying to do at the moment. Commented Mar 4, 2016 at 12:12
  • @fubo User does not asks how to iterate over <b>properties</b> of an instance. Commented Mar 4, 2016 at 12:12
  • 1
    Possible duplicate of How do I get all instances of all loaded types that implement a given interface? Commented Mar 4, 2016 at 15:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.