Long time since the first problem..

This is a problem I’ve faced lately while developing in C# to have some statistics.

I’ve created a project that does something (of course !! :D)

and created a number of classes with some inheritance tree, where some classes inherit from others, anyway, after that i needed to count the number of instances created from each class…

pretty simple, just add a counter to the class, add increment/decrement methodology in the constructor/destructor.

but. kind of… I’m lazy to open each class, add those members and add them to the newly created, not new!

So.. let’s make use of Inheritances, Create a base class “Countable Object” where all what you need is a private static dictionary which will hold all the type as a key, count as a value, with a constructor that increments the value of the type created.

Class CountableObject
{
   private static Dictionary InstancesCounter = new Dictionary();
   public CountableObject()
   {
       InstancesCounter[this.GetType()]++;
   }
   public ~CountableObject()
   {
       InstancesCounter[this.GetType()]--;
   }
}

I didn’t include the check if that key exists of not, just to simplify things, but I assure you’ll have an exception on running the above snippet😉

Then…
just make all your classes to inherit this class & call the base constructor and you have the numbers!

The Next step where I found the major problem..

How to expose those numbers..

I can make the dictionary public and the numbers is extracted as


int count = CountableObject.InstancesCount[TypeOf(ChildClass)];

with another exception to be handled here😀

despite this exposure is not good, and it’s a bit complex to be used, one of the side effects will be the following usage


int count = ChildClass.InstancesCount[TypeOf(OtherChildClass)];

where each class will have the count of all instances of all the sibling classes!!

and simply I can’t hide members from being inherited.

I had an idea to create a public property “Count” in the base class that returns the count of each type, using the polymorphism of “GetType()” as in the incrementing methodology.

but. i couldn’t find any method that return the type of the class in a static member.

or to be more accurate there is a method that gets the type, but it always returns the base type, where it is declared in..


MethodBase.GetCurrentMethod().DeclaringType

there is another way to get the type using stack trace, but it also gets the base class…

So… any ideas ?!😀😀

my proposed solutions was

1-Create the static property in each class to get the count. NO !! that’s what I avoided at the beginning !!😀

2-Create a separate class to hold this dictionary. and that what I’ve done.

class Statistics, and the dictionary is filled by the CountableObject class using a function and to get the Count use a static function

Class Statistics
{
   public static void IncrementType(Type type);
   public static void DecrementType(Type type);
   public static int GetNumberOfInstances(Type type);
}

so, now I don’t need to add the counter variable and its logic to each class i create, just inherit from the CountableObject class, and I have all the statistics…

any better idea ?!