using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Tiobon.Core.Common.DB.Dapper.Enums;
namespace Tiobon.Core.Common.DB.Dapper.Extensions
{
public static class LambdaExtensions
{
///
/// 获取对象表达式指定属性的值
/// 如获取:Out_Scheduling对象的ID或基他字段
///
///
/// 格式 Expression>sch=x=>new {x.v1,x.v2} or x=>x.v1 解析里面的值返回为数组
///
public static string[] GetExpressionToArray(this Expression> expression)
{
string[] propertyNames = null;
if (expression.Body is MemberExpression)
{
propertyNames = new string[] { ((MemberExpression)expression.Body).Member.Name };
}
else
{
propertyNames = expression.GetExpressionProperty().Distinct().ToArray();
}
return propertyNames;
}
///
/// 属性判断待完
///
///
///
public static IEnumerable GetGenericProperties(this Type type)
{
return type.GetProperties().GetGenericProperties();
}
///
/// 属性判断待完
///
///
///
public static IEnumerable GetGenericProperties(this IEnumerable properties)
{
return properties.Where(x => !x.PropertyType.IsGenericType && x.PropertyType.GetInterface("IList") == null || x.PropertyType.GetInterface("IEnumerable", false) == null);
}
}
public class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary map;
public ParameterRebinder(Dictionary map)
{
this.map = map ?? new Dictionary();
}
public static Expression ReplaceParameters(Dictionary map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
}
}