Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP inlist() in c#
Message
From
02/11/2008 12:11:00
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01320903
Message ID:
01359002
Views:
68
>I also just discovered that you can apply extension methods to enums !

I just discovered you can apply extension methods to delegates ...

I haven't figured out delegates with parameters, but I use this to compare different ways of doing things

For example, I want to time the execution of a void method that takes no parameters
using GregoryAdam.Base.ExtensionMethods;

	class test
	{
		static void Main(string[] args)
		{
			((delegateVoid)TestLoop).ExecuteTimed(true);
		}

		
		static void TestLoop()
		{
			for (int i = int.MaxValue/2; --i != 0; )
				;
		}
	}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// delegates
namespace GregoryAdam.Base.ExtensionMethods
{
	public delegate void delegateVoid();

	public static partial class ExtensionMethods
	{

		#region ExecuteTimed
		

		private static Stack<DateTime> Times = new Stack<DateTime>();
		private static Stack<string> Subject = new Stack<string>();
		//-------------------------------------------------------------------------
		public static void ExecuteTimed(this delegateVoid function)
		{
			ExecuteTimed(function, false);
		}

		public static void ExecuteTimed(this delegateVoid function, bool pause)
		{
			Start(function.GetInvocationList()[0].Method.Name + "()");
			function();
			End(pause);
		}

		//-------------------------------------------------------------------------
		private static void Start(string subject)
		{
			lock (Times)
			{
				Subject.Push(subject);
				Times.Push(DateTime.Now);
			}
		}
		//-------------------------------------------------------------------------
		private static void End()
		{
			End(false);
		}
		//-------------------------------------------------------------------------
		private static void End(bool pause)
		{
			TimeSpan elapsed;
			string subject;

			lock (Times)
			{
				if (Times.Count == 0)
				{
					System.Diagnostics.Debug.Assert(false, "Time Stack empty");
					return;
				}

				elapsed = DateTime.Now - Times.Pop();
				subject = Subject.Pop();
			}

			Console.WriteLine("{0} : {1:00}:{2:00}:{3:00}.{4:00}",
						subject,
						elapsed.Hours,
						elapsed.Minutes,
						elapsed.Seconds,
						elapsed.Milliseconds / 10
						);

			if (pause)
			{
				Console.WriteLine("Enter to continue");
				Console.ReadLine();
			}
		}
		//-------------------------------------------------------------------------
		#endregion

	}
}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform