public void ParameterReplaceVisitorTest() { var p1 = Expression.Parameter(typeof(int)); var param = new Builder.ParameterReplaceVisitor(p1, p1); Expression <Func <int, object> > p = (a) => null; param.Visit(p); param.Visit(p1); Assert.AreNotEqual(param, null); }
public static Expression <Func <T, object> > CombineSelectorsToNewObject <T>(List <FieldDefinition <T> > selectors, Type createdType) { var param = Expression.Parameter(typeof(T), "x"); var arguments = new List <Expression>(); foreach (var selector in selectors) { var replace = new ParameterReplaceVisitor(selector.Selector.Parameters[0], param); var fieldName = SanitizeCSharpIdentifier(selector.Name); var property = createdType.GetField(fieldName); if (property == null) { throw new ApplicationException($"Could not find field {fieldName} for type {createdType}."); } var replacedParamExpression = replace.Visit(selector.Selector.Body); if (replacedParamExpression == null) { throw new ApplicationException($"Could Visit and Convert expression: {selector.Selector}"); } replacedParamExpression = Expression.Convert(replacedParamExpression, GetNullableType(selector.Type)); arguments.Add(replacedParamExpression); } var ctor = createdType.GetConstructor(selectors.Select(a => a.Type).ToArray()); if (ctor == null) { throw new ApplicationException( $"Could not get ctor for created type: {createdType}, with args: {string.Join<Type>(",", selectors.Select(a => a.Type).ToArray())}"); } var ctorExpr = Expression.New(ctor, arguments.ToArray()); return(Expression.Lambda <Func <T, object> >(ctorExpr, param)); }