As a response to this question I writed an extension to extend the Object class, the code is as follow
using System; using System.Reflection; using System.Diagnostics; using System.Collections.Generic; namespace Extension { static public class ObjectExtension { static public TimeSpan MesurePeriodeExecution(this object o,
Assembly assembly,
string typeinstance,
string methode, params object[] parametres) { Type t = assembly.GetType(typeinstance, true, true); List<Type> listetypes = new List<Type>(); foreach (var item in parametres) { listetypes.Add(item.GetType()); } object instance = Activator.CreateInstance(t); MethodInfo m = t.GetMethod(methode,listetypes.ToArray()); Stopwatch stopWatch = new Stopwatch(); if (m.IsStatic) { stopWatch.Start(); m.Invoke(null, parametres); stopWatch.Stop(); } else { stopWatch.Start(); m.Invoke(instance, parametres); stopWatch.Stop(); } return stopWatch.Elapsed; } } }
Of Corse, this takes only instance and static non generic methods,
you could extend this code to include generic methods
No comments:
Post a Comment