public void VisitCorrectlyTransformsParameterExpression() { IStructureService service = Substitute.For <IStructureService>(); service.TranslateType(typeof(ClassA)).Returns(typeof(ClassB)); TypeVisitor visitor = new TypeVisitor(service) { TypeVerifiers = new ITypeVerifier[0] }; Expression input = Expression.Parameter(typeof(ClassA)); Expression expected = Expression.Parameter(typeof(ClassB)); Expression actual = visitor.Visit(input); AssertAreEqualExpressions(expected, actual); }
protected override Expression VisitParameter(ParameterExpression node) { _structureService.VerifyType(node.Type, TypeVerifiers); // To preserve the referential equality of parameter expressions // the function must return the exact same output for the same input. // Not doing this would result in the expression not being compile-able. if (!_variableCache.ContainsKey(node)) { Random random = new Random(); Type translatedType = _structureService.TranslateType(node.Type); ParameterExpression newParameter = Expression.Parameter(translatedType, random.Next().ToString()); _variableCache.Add(node, newParameter); return(newParameter); } return(_variableCache[node]); }