Click here to Skip to main content
15,891,316 members
Articles / Database Development / SQL Server

DynamicParser: How to parse Delegates and Dynamic Lambda Expressions and convert them into Expression Trees

Rate me:
Please Sign up or sign in to vote.
4.78/5 (29 votes)
29 Aug 2012CPOL14 min read 87.3K   872   97  
Describes how to use C# dynamics to convert a delegate into an expression tree
// ========================================================
namespace MB.Tools
{
	using global::System.Diagnostics;
	using global::System;
	using global::System.Reflection;

	// =====================================================
	public static class DebugHelper
	{
		public static MethodBase CallingMethod()
		{
			try {
				var stack = new StackTrace();
				var frame = stack == null ? null : stack.GetFrame( 2 );
				var method = frame == null ? null : frame.GetMethod();

				return method;
			}
			catch { } // This is the easy way to intercept, at the cost of a first-chance exception
			return null;
		}
		public static string CalledFrom()
		{
			try {
				var stack = new StackTrace();
				var frame = stack == null ? null : stack.GetFrame( 2 );
				var method = frame == null ? null : frame.GetMethod();

				return string.Format( "{0}.{1}", method.DeclaringType.Name, method.Name );
			}
			catch { } // This is the easy way to intercept, at the cost of a first-chance exception
			return "n/a";
		}
	}

	// =====================================================
	public static class DEBUG
	{
		static char[] separators = new char[] { '\n' };

		[Conditional( "DEBUG" )]
		public static void Write( string message )
		{
			if( message == null ) return;

			string[] strs = message.Split( separators, StringSplitOptions.None );
			for( int i = 0; i < strs.Length; i++ ) {
				Debug.Write( strs[i] );
				if( i < ( strs.Length - 1 ) ) Debug.WriteLine( "" );
			}
		}

		[Conditional( "DEBUG" )]
		public static void WriteLine()
		{
			Debug.WriteLine( "" );
		}

		[Conditional( "DEBUG" )]
		public static void WriteLine( string message )
		{
			Write( message );
			Debug.WriteLine( "" );
		}

		[Conditional( "DEBUG" )]
		public static void Write( string format, params object[] args )
		{
			if( format == null ) return;
			if( args == null || args.Length == 0 ) Write( format );

			string message = string.Format( format, args );
			Write( message );
		}

		[Conditional( "DEBUG" )]
		public static void WriteLine( string format, params object[] args )
		{
			Write( format, args );
			Debug.WriteLine( "" );
		}

		[Conditional( "DEBUG" )]
		public static void Indent()
		{
			Debug.Indent();
		}

		[Conditional( "DEBUG" )]
		public static void Indent( string message )
		{
			Debug.Indent();
			Write( message );
		}

		[Conditional( "DEBUG" )]
		public static void Indent( string format, params object[] args )
		{
			Debug.Indent();
			Write( format, args );
		}

		[Conditional( "DEBUG" )]
		public static void IndentLine( string message )
		{
			Debug.Indent();
			WriteLine( message );
		}

		[Conditional( "DEBUG" )]
		public static void IndentLine( string format, params object[] args )
		{
			Debug.Indent();
			WriteLine( format, args );
		}

		[Conditional( "DEBUG" )]
		public static void Unindent()
		{
			Debug.Unindent();
		}

		[Conditional( "DEBUG" )]
		public static void Unindent( string message )
		{
			Write( message );
			Debug.Unindent();
		}

		[Conditional( "DEBUG" )]
		public static void Unindent( string format, params object[] args )
		{
			Write( format, args );
			Debug.Unindent();
		}

		[Conditional( "DEBUG" )]
		public static void UnindentLine( string message )
		{
			WriteLine( message );
			Debug.Unindent();
		}

		[Conditional( "DEBUG" )]
		public static void UnindentLine( string format, params object[] args )
		{
			WriteLine( format, args );
			Debug.Unindent();
		}

		[Conditional( "DEBUG" )]
		public static void PrintException( Exception e )
		{
			while( e != null ) {
				WriteLine( "\n=======================================" );
				WriteLine( "\nException: {0}", e.Message );
				WriteLine( "\nStack Trace: \n{0}", e.StackTrace );
				e = e.InnerException;
			}
			WriteLine();
		}
	}
}
// ========================================================

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Spain Spain
mbarbac has worked in start-ups, multinational tech companies, and consulting ones, serving as CIO, CTO, SW Development Director, and Consulting Director, among many other roles.

Solving complex puzzles and getting out of them business value has ever been among his main interests - and that's why he has spent his latest 25 years trying to combine his degree in Theoretical Physics with his MBA... and he is still trying to figure out how all these things can fit together.

Even if flying a lot across many countries, along with the long working days that are customary in IT management and Consultancy, he can say that, after all, he lives in Spain (at least the weekends).

Comments and Discussions