When ever I load a new class I measure the time it takes to setup, initializing and load that class, it helps me debug the time it takes to complete actions within my application.
Here I have a pretty nice feature that does it all for me.
public static T CreateInstanceOf<T>() where T : new() { var stopwatch = Stopwatch.StartNew(); var result = new T(); stopwatch.Stop(); Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]"); return result; }
It also has really simple usage, like so..
ConfigHandler = CoreUtilities.CreateInstanceOf<ConfigHandler>();
But, the issues come when I want to load the class. Now you could say that I could put this in the constructor and have my above method measure the time the method takes as well as initiating the class, but as C# has stated, constructors aren’t for calling methods.
While I want to measure the time effiently, I also don’t want to break any language rules that are recommended to follow, can anyone think of a work around.
Here is all I do to load the classes, just a simple method.
ConfigHandler.Load("resources/config/server.config.json");
I could just add a stopwatch between every 2 lines of everything I initialize, but lets be honest, who wants to do that, especially when you have around 30 classes to initiate in your project.