Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
VFP inlist() in c#
Message
De
10/11/2008 20:25:01
 
 
À
02/11/2008 12:11:00
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01320903
Message ID:
01360994
Vues:
35
That sure looks pretty cool ... I'll need to really mess around with it a bit to see if I can get my head around it though. Interesting! =0)

~~Bonnie



>>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
>
>	}
>}
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform