Click here to Skip to main content
15,883,809 members
Articles / Programming Languages / C# 4.0

Flexpressions

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
10 Oct 2012CPOL10 min read 39.9K   507   47  
An intuitive-fluent API for generating Linq Expressions.
<#@ Template Debug="True" Hostspecific="True" Language="C#" #>
<#@ Assembly Name="System.Core" #>
<#@ Import Namespace="System.Linq" #>
<#@ Output Extension=".cs" #>
<#@ Include File="Common.ttinc" #>using System;
using System.Linq.Expressions;

namespace Flexpressions
{
	public sealed partial class Switch<TParent, R>
	{
<#
/********************************************************************************************/
/*									Case Methods											*/
/********************************************************************************************/
#>
		#region Case Methods

<#
for (int i = 1; i <= MAX_NUMBER_OF_GENERIC_PARAMETERS; ++i)
{
	var argumentTypeList = GetGenericTypeList(i, GENERIC_TYPE_PARAMETER_FORMAT);
#>
		/// <summary>
		/// Creates a new case statement for the switch statement.
		/// </summary>
<#
	for (var j = 1; j <= i; j++)
	{
#>
		/// <typeparam name="<#= string.Format(GENERIC_TYPE_PARAMETER_FORMAT, j) #>">The type of the <#= ToOrdinalNumbers(j) #> argument.</typeparam>
<#
	}
#>
		/// <param name="caseValue">The value of the case.</param>
		/// <returns>The <see cref="SwitchCase&lt;TParent, R&gt;"/> to create the case.</returns>
		/// <exception cref="ArgumentNullException">When the <paramref name="caseValue"/> is null, the exception is thrown.</exception>
		public SwitchCase<Switch<TParent, R>, R> Case<<#= argumentTypeList #>>(Expression<Func<<#= argumentTypeList #>, R>> caseValue)
		{
			if (caseValue == null)
				throw new ArgumentNullException("caseValue");

			return this.CaseSafe(caseValue.Body);
		}
<#
}
#>

		#endregion Case Methods
	}
}

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
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions