private static Expression CreateTupleSelector( Expression body, Expression[] memberSelectors) { Type[] memberTypes = memberSelectors.Select(e => e.Type).ToArray(); Type tupleType = TupleTypeHelper.CreateTupleType(memberTypes); var helper = new TupleKeyInfoHelper(tupleType); body = helper.CreateKeyFactoryExpression(memberSelectors); return(body); }
public void TupleKeyInfoHelper_KeyFactory() { TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple <int, string>)); Expression factory = builder.CreateKeyFactoryExpression( Expression.Constant(1, typeof(int)), Expression.Constant("2", typeof(string))); var result = Expression.Lambda(factory).Compile().DynamicInvoke() as Tuple <int, string>; Assert.IsNotNull(result); Assert.AreEqual(1, result.Item1); Assert.AreEqual("2", result.Item2); }
public static LambdaExpression CreateMemberSelectorLambdaExpression( Expression[] members, params ParameterExpression[] parameters) { if (members.Length == 1) { return(Expression.Lambda(members[0], parameters)); } else { Type[] tupleTypes = members.Select(m => m.Type).ToArray(); Type tuple = TupleTypeHelper.CreateTupleType(tupleTypes); var helper = new TupleKeyInfoHelper(tuple); return(Expression.Lambda( helper.CreateKeyFactoryExpression(members), parameters)); } }
public void TupleKeyInfoHelper_KeyFactoryLarge() { TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple <int, int, int, int, int, int, int, Tuple <int, int> >)); Expression factory = builder.CreateKeyFactoryExpression( Enumerable .Range(1, 9) .Select(x => Expression.Constant(x, typeof(int))) .ToArray()); var result = Expression.Lambda(factory).Compile().DynamicInvoke() as Tuple <int, int, int, int, int, int, int, Tuple <int, int> >; Assert.IsNotNull(result); Assert.AreEqual(1, result.Item1); Assert.AreEqual(2, result.Item2); Assert.AreEqual(8, result.Rest.Item1); Assert.AreEqual(9, result.Rest.Item2); }