/// <summary> /// Get all the attributes of decorated method /// </summary> /// <param name="method">Method for which attributes need to be retrieved</param> /// <returns>All the attributes of decorated method</returns> private static SpeedTestMethodAttribute GetSpeedTestMethodAttribute(MethodInfo method) { SpeedTestMethodAttribute methodAttribute = null; foreach (var attribute in method.GetCustomAttributes(true)) { if (attribute is SpeedTestMethodAttribute) { methodAttribute = attribute as SpeedTestMethodAttribute; break; } } return(methodAttribute); }
/// <summary> /// Execute all the methods which are decorated with "SpeedTest" class and "SpeedTestMethodAttribute" type /// </summary> /// <param name="asm"> Fully qualified Assembly name</param> private static void RunSpeedTestOnDecoratedMethods(string asm) { // Load asm var assembly = Assembly.LoadFile(asm); //Get all the types var AsmTypes = assembly.GetTypes(); foreach (var type in AsmTypes) { // Check if it is decorated with SpeedTest class if (IsClassDecoratedWithSpeedTest(type)) { object instance = Activator.CreateInstance(type); var methods = type.GetMethods(); foreach (var method in methods) { var tempMethod = method; SpeedTestMethodAttribute methodAttrib = GetSpeedTestMethodAttribute(tempMethod); if (methodAttrib != null) { string displayMsg = string.Format("Method Name: {0}{1}{2}", tempMethod.Name, Environment.NewLine, methodAttrib.Message); // Run Init method of SpeedTest method if (methods.Any(x => string.Equals(x.Name, methodAttrib.InitializationMethod))) { type.GetMethod(methodAttrib.InitializationMethod).Invoke(instance, null); } TestSpeed(displayMsg, methodAttrib.NoOfIterations, () => tempMethod.Invoke(instance, null)); //Run Reset method of SpeedTest method if (methods.Any(x => string.Equals(x.Name, methodAttrib.ResetMethod))) { type.GetMethod(methodAttrib.ResetMethod).Invoke(instance, null); } } } } } }