GithubHelp home page GithubHelp logo

classcounter's Introduction

ClassCounter

Simple example to count the number of times a class has been created and how many instances of that class are still alive.

How to Run

git clone https://github.com/mwcaisse/ClassCounter.git
cd ./ClassCounter/src/ClassCounter
dotnet run 150

Sample Output

  NonInheritableCountable: Created:  600  Alive:   0
                ImCounted: Created:  150  Alive:   0
             StayingAlive: Created: 1200  Alive: 400
          ImCountedAsWell: Created:  300  Alive:   0

Implementation

Class Inheritance

Simply inherit from ClassCounterBase on the class(es) you wish to enable counting on.

public class ClassIWantToCountWithInheritance : ClassCounterBase 
{

}

####Advantages

  1. Only need to add the base class to enable counting, no need to add code to the constructor and deconstructor. Any changes made to the API will not require updating the class.

####Disadvantages

  1. You need to inherit from the BaseClassCounter class, which will not allow you to inherit from another class.
  2. Need to modify the class to enable instance counting.

Without Inheritance

Add the ClassCounter.InstanceCreated to the constructor and ClassCounter.InstanceRemoved to the deconstructor.

public class ClassIWantToCount 
{

	public ClassIWantToCount() 
	{
		ClassCounter.InstanceCreated(this);
	}
	
	~ClassIWantToCount() 
	{
		ClassCounter.InstanceRemoved(this);
	}
}

####Advantages

  1. You are free to inherit from which ever class you choose

####Disadvantages

  1. You need to add 2 lines manually to the constructor + deconstructor, and will need to change them manually if ClassCounter API changes.
  2. Need to modify the class to enable instance counting.

Other Considerations

Both of these methods require you modify the class directly to enable instance counting. Ideally it would be nice to have a RegisterForInstanceCounting(Type t) method that enables instance counting for that type without modifying the class. I was not able to find a way to do this without modifying the .NET runtime.

Both methods also make use of a static class to perform the counting. This doesn't allow you to have different objects for counting different classes. If that is desired it wouldn't be too hard to modify ClassCounter to be non-static. In doing so you would have to pass the instance of ClassCounter you wish to use to count to the constructor of each object. Depending on the use this may or may not be desirable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.