3

Is it possible to list all instances of a class? I don't mean definitions, I mean allocated declarations of a class. Reflection is typically used to get a list of classes defined but that is not what I am asking for.

This is for educational purposes. I want to list all instances of a specific class (such as the string class) to learn about that class. Actually, I want to list all instances of the WPF Binding class since that will help me understand how bindings work in WPF. This however is not a WPF question.

Update: I don't think that c# - How do I get all instances of all loaded types that implement a given interface? answers the question. It says that walking the heap or stack might be possible but there are no details of how to do that. I don't need detailed code but I am asking for more guidance than saying to walk the heap or stack.

I have found the following suggestions:

I am not familiar with any of those but I can learn if it will help here. I will however appreciate reasonable assurance that it is worth the time to pursue something.

Also, I know C++ and I am familiar with Detours (as in my Detours Introduction). If Detours can be used then a hint about what to detour would help.

Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • 1
    Sounds like you're wanting to dump the contents of memory? – mason Sep 10 '18 at 19:40
  • 1
    Here's a fairly old question on the MSDN forums that says no, this isn't possible: [Getting all running instances of a specific class type using reflection](https://social.msdn.microsoft.com/Forums/vstudio/en-US/f9e984e3-a47b-42b0-b9bd-1f1c55e4de96/getting-all-running-instances-of-a-specific-class-type-using-reflection?forum=netfxbcl). I doubt that's changed in the meantime, but it might have. – Rup Sep 10 '18 at 19:44
  • @Pac0 That *is* a duplicate, regardless of the interface filter – Camilo Terevinto Sep 10 '18 at 19:45
  • As a language or framework feature, this doesn't seem to exist. However, you can use some custom code to keep references yourself, that's basically the way to do it. – Pac0 Sep 10 '18 at 19:46
  • @CamiloTerevinto indeed, the more I read about it, the more I see that the answers actually apply to this question. – Pac0 Sep 10 '18 at 19:47
  • @CamiloTerevinto this is not a duplicate - listing instances for learning is a non-trivial task. – too Sep 10 '18 at 23:45
  • @user34660: for non-sealed classes you can create a new inherited class and add a counter (private static int InstanceCount) or a static list of all instances (private static List and create constructor overloads which add each instance to this list. Beware: items will not be garbage collected so can cause memory leaks, use only for debugging and also be careful with classes implementing IDisposable. When needed, you can peek what was created during debugging. Alternatively use a profiler like dotTrace or dotMemory which will do the counting for you. – too Sep 10 '18 at 23:45
  • @mason, if you are suggesting the type of memory dump that I often used 40 years ago then I am hoping not to do that. There was a time when I worked with hexadecimal in memory dumps so much that I mistakenly tried to balance my checking account using hexadecimal. – Sam Hobbs Sep 11 '18 at 23:34
  • @Rup, thank you for finding that. I did try to find previous answers. That other thread does not say anything useful except that it is not possible. – Sam Hobbs Sep 11 '18 at 23:36
  • @Pac0, if keeping references means doing that when the object is created, then that is not possible. I was not as clear as I should have been but this is for objects that WPF creates internally. – Sam Hobbs Sep 11 '18 at 23:38
  • @too, I do not understand how to get WPF to use the inherited class. – Sam Hobbs Sep 11 '18 at 23:41
  • Yes, it seems the only "positive and constructive" does not apply for automatic instantiations. I feel it quite surprising indeed that there is no way to do that. Even more surprised for a managed language. Makes me wonder, how do the garbage collector *does* keep track of them. – Pac0 Sep 12 '18 at 00:49
  • Here is a question which has the same duplicate target, and contains interesting suggestions : https://stackoverflow.com/questions/40615126/how-to-get-a-reference-to-an-instance-of-a-class-where-there-is-no-known-object (in the comment sections). For the record, that would be try to create a profiler, or use code injection techniques to be able to hijack the constructors and implement the manual registering of instances as suggested. Also related (but a bit stall : https://stackoverflow.com/questions/24241/code-injection-with-c-sharp) – Pac0 Sep 12 '18 at 01:03

0 Answers0