- GenericWcfWithPocos.zip
- GenericWcfWithPocos
- CodeGeneration
- EdmxLibrary
- Templates
- GenericWcfWithPocos.sln
- GenericWcfWithPocos.suo
- GenericWcfWithPocos.vsmdi
- Library
- AutoMapper.dll
- Local.testsettings
- Source Code
- Common
- AdventureWorks.Interface
- AdventureWorksModel
- SearchHelpers
- Server
- AdventureWorksEntityModel
- AdventureWorksService
- ServerRepository
- Tests
- EntityServiceTests
- TraceAndTestImpact.testsettings
|
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.
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.