/// <summary> /// 访问子表达式树 /// </summary> /// <param name="node">访问表达式树</param> /// <returns> /// 改变表达式,如果它或它的任何子表达式被修改; 否则,返回原始表达式。 /// </returns> protected override Expression VisitMember(MemberExpression node) { var expr = Visit(node.Expression); if (expr != null && expr.Type != node.Type) { if (_mapper == null) { _mapper = ExpressionMapper.GetMapper(node.Member.DeclaringType, _destinationType); } Expression expDest; // 认为原始类是简单属性(不是子对象)。 if (!expr.Type.IsValueType && expr.Type != typeof(string) && expr.NodeType != ExpressionType.Parameter && expr.NodeType != ExpressionType.Constant) { var subExp = ExpressionMapper.GetMapper(node.Member.DeclaringType, expr.Type); expDest = subExp.GetLambdaDest(node.Member.Name); return(AnalyseDestExpression(expr, expDest)); } expDest = _mapper.GetLambdaDest(node.Member.Name); if (expDest != null) { return(AnalyseDestExpression(expr, expDest)); } } return(base.VisitMember(node)); }
public void Map_NoActionException_Exception() { ExpressionMapper.GetMapper <ClassSource, ClassDest>().AfterMap(null); ExpressionMapper.Initialize(); new ClassSource().Map <ClassSource, ClassDest>(); Clean(); }
private static IQueryable <TDest> GetSelect <TSource, TDest>(IQueryable <TSource> query, string mapperName) where TSource : class where TDest : class { // 不需要mapper if (typeof(TSource) == typeof(TDest)) { return((IQueryable <TDest>)query); } return(query.Select(ExpressionMapper.GetMapper <TSource, TDest>(mapperName).GetLambdaExpression())); }
public void Map_NoActionException_Exception() { Assert.Throws <NoActionAfterMappingException>(() => { ExpressionMapper.GetMapper <ClassSource, ClassDest>().AfterMap(null); ExpressionMapper.Initialize(); new ClassSource().Map <ClassSource, ClassDest>(); Clean(); }); }
private static TQueryable CreateSortedMethodCall <TSource, TDest, TQueryable>(IQueryable <TSource> query, string methodName, string sortedPropertySourceName) where TSource : class where TDest : class where TQueryable : class, IQueryable <TSource> { var mapper = ExpressionMapper.GetMapper <TSource, TDest>(); var prop = mapper.GetLambdaDest(sortedPropertySourceName); var lambda = mapper.GetSortedExpression(sortedPropertySourceName); var resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TSource), prop.Type }, query.Expression, Expression.Quote(lambda)); return(query.Provider.CreateQuery <TSource>(resultExp) as TQueryable); }
public void TestExpressionMapper_Int() { //Arrange var em = new ExpressionMapper <TestRecord, Test_Int>(); var tr = new TestRecord { Steps = new List <TestStepRecord> { new TestStepRecord { Step = "Test" } } }; //Act var t = em.GetMapper()(tr); //Assert Assert.IsTrue(t.Steps.Count == 1); Assert.IsTrue(t.NotNullSetterCalled); }
public void TestExpressionMapper_Int() { //Arrange var em = new ExpressionMapper<TestRecord, Test_Int>(); var tr = new TestRecord { Steps = new List<TestStepRecord> { new TestStepRecord {Step = "Test" } } }; //Act var t = em.GetMapper()(tr); //Assert Assert.IsTrue(t.Steps.Count == 1); Assert.IsTrue(t.NotNullSetterCalled); }