/// <summary> /// Rewrites Is expression. /// </summary> /// <param name="asExpression">Expression to rewrite.</param> /// <returns>Expression rewritten as code snippet.</returns> private CodeSnippetExpression RewriteIsExpression(CodeIsExpression asExpression) { using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) { sw.Write("("); // VB.net doesn't allow direct comparision of types such as // c.Id Is Integer // this needs to be rewritten to : // Typeof DirectCast(c.Id,Object) Is Integer // the type comparision operator can only be used on reference types, // hence the cast to Object before the TypeOf operator is invoked. sw.Write("TypeOf DirectCast("); this.generator.GenerateCodeFromExpression(asExpression.Source, sw, null); sw.Write(" , Object )"); sw.Write(" Is "); // Handle the CodeTypeReference issue with Byte[] if (asExpression.TargetType.Type.BaseType == "System.Byte") { sw.Write("Byte()"); } else { this.generator.GenerateCodeFromExpression(asExpression.TargetType, sw, null); } sw.Write(")"); return(new CodeSnippetExpression(sw.ToString())); } }
/// <summary> /// Rewrites Is expression. /// </summary> /// <param name="asExpression">Expression to rewrite.</param> /// <returns>Expression rewritten as code snippet.</returns> private CodeSnippetExpression RewriteIsExpression(CodeIsExpression asExpression) { using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) { sw.Write("("); this.generator.GenerateCodeFromExpression(asExpression.Source, sw, null); sw.Write(" is "); this.generator.GenerateCodeFromExpression(asExpression.TargetType, sw, null); sw.Write(")"); return(new CodeSnippetExpression(sw.ToString())); } }
/// <summary> /// Rewrites CodeExpression /// </summary> /// <param name="e">A CodeExpression to be rewritten.</param> /// <param name="didRewrite">A value which will be set to true if the rewriting returned a new object.</param> /// <returns>Rewritten CodeExpression.</returns> protected override CodeExpression Rewrite(CodeExpression e, ref bool didRewrite) { CodeLambdaExpression lambdaExpression = e as CodeLambdaExpression; if (lambdaExpression != null) { didRewrite = true; return(this.RewriteLambdaExpression(lambdaExpression)); } CodeQueryExpression queryExpression = e as CodeQueryExpression; if (queryExpression != null) { didRewrite = true; return(this.RewriteComprehensionExpression(queryExpression)); } CodeCreateAndInitializeObjectExpression objectCreateExpression = e as CodeCreateAndInitializeObjectExpression; if (objectCreateExpression != null) { didRewrite = true; return(this.RewriteCreateAndInitializeObjectExpression(objectCreateExpression)); } CodeArrayInitializerExpression arrayInitializer = e as CodeArrayInitializerExpression; if (arrayInitializer != null) { didRewrite = true; return(this.RewriteArrayInitializerExpression(arrayInitializer)); } CodeExclusiveOrExpression exclusiveOrExpression = e as CodeExclusiveOrExpression; if (exclusiveOrExpression != null) { didRewrite = true; return(this.RewriteExclusiveOrExpression(exclusiveOrExpression)); } CodeAsExpression asExpression = e as CodeAsExpression; if (asExpression != null) { didRewrite = true; return(this.RewriteAsExpression(asExpression)); } CodeIsExpression isExpression = e as CodeIsExpression; if (isExpression != null) { didRewrite = true; return(this.RewriteIsExpression(isExpression)); } CodePrimitiveExpression primitiveExpression = e as CodePrimitiveExpression; if (primitiveExpression != null) { return(this.RewritePrimitiveExpression(primitiveExpression, ref didRewrite)); } var ternaryExpression = e as CodeTernaryExpression; if (ternaryExpression != null) { didRewrite = true; return(this.RewriteTernaryExpression(ternaryExpression)); } var anonymousArray = e as CodeAnonymousArrayExpression; if (anonymousArray != null) { didRewrite = true; return(this.RewriteAnonymousArrayExpression(anonymousArray)); } return(base.Rewrite(e, ref didRewrite)); }