Click here to Skip to main content
15,896,915 members
Articles / Programming Languages / C#

The Power of a POC

Rate me:
Please Sign up or sign in to vote.
4.42/5 (6 votes)
26 Apr 2011CPOL13 min read 50K   358   19  
The importance of creating a POC as the first step to your next adventure
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace EntityServiceTests
{
	public class LambdaHelper
	{
		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="exp"></param>
		/// <returns></returns>
		public static string GetName<T>(Expression<Func<T, object>> exp)
		{
			return GetName<T>(exp.Body, 0);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="exp"></param>
		/// <param name="level"></param>
		/// <returns></returns>
		private static string GetName<T>(Expression exp, int level)
		{
			string name = string.Empty;

			if (exp.Type.Equals(typeof(T)) && level > 0)
				return name;

			if (exp is ParameterExpression)
			{
				ParameterExpression parameterExpression = (ParameterExpression)exp;
				return parameterExpression.Type.Name;
			}

			MemberExpression memberExpression = null;
			if (exp is MemberExpression)
			{
				memberExpression = (MemberExpression)exp;
				if (memberExpression.Expression != null)
				{
					string tempName = GetName<T>(memberExpression.Expression, level + 1);
					name += tempName;
				}
			}
			else if (exp is UnaryExpression)
			{
				UnaryExpression unaryExpression = (UnaryExpression)exp;
				memberExpression = (unaryExpression.Operand as MemberExpression);
				if (memberExpression != null)
				{
					string tempName = GetName<T>(memberExpression.Expression, level + 1);
					name += tempName;
				}
			}

		    if (memberExpression != null)
			{
				PropertyInfo pinfo = memberExpression.Member as PropertyInfo;
				if (pinfo != null)
				{
					return (string.IsNullOrEmpty(name))
						? pinfo.Name
						: string.Format("{0}.{1}", name, pinfo.Name);
				}
			}
            else if (exp is MethodCallExpression)
            {
                StringBuilder sb = new StringBuilder();
                MethodCallExpression methodCallExpression = (exp as MethodCallExpression);
                foreach (Expression expression in methodCallExpression.Arguments)
                {
                    sb.AppendFormat("{0}.", GetName<T>(expression, level + 1));
                }

                sb.Length--;
                return sb.ToString();
            }

			return null;
		}
	}
}

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
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions