MSBuild Task Generator: Part 6. Conditional statements in the CodeDOM.

Yesterday we introduced the CodeDOM and covered some fundamentals.  Creating a namespace, a class, a member method and a method body.  Today we’re going to cover conditionals.  I know I promised loops too but conditionals is taking enough space that if I tried to do loops too this would turn into a novel, not a blog post.  Loops tomorrow – I promise!


A conditional statement ( if(Boolean) { … } else { … } ) is nothing more then an expression that has a Boolean result and two collections of statements (the code executed if the test is true or false).


We’ll skip the namespace and class declarations from yesterday – please reference those if necessary.  For today create a method named “IfSample” that takes 2 parameters (string lhs, string rhs).  The purpose of the method is to print a string stating whether or not the string parameters are equal (using String.Equals as our test).


Since we are going to be referencing these two parameters “lhs” and “rhs” a few times let’s define them:


CodeVariableReferenceExpression lhs = new CodeVariableReferenceExpression("lhs");

CodeVariableReferenceExpression rhs = new CodeVariableReferenceExpression("rhs");


Now we create our member method:


CodeMemberMethod ifSample = new CodeMemberMethod();

ifSample.Name = "IfSample";

ifSample.ReturnType = new CodeTypeReference(typeof(void));

ifSample.Attributes = MemberAttributes.Public;

ifSample.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), lhs.VariableName));

ifSample.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), rhs.VariableName));


Notice that we are adding a few parameters to the function.  These are variables of type string with the names “lhs” and “rhs” (also notice that I use the VariableName property to name them instead of hardcoding the string literals all throughout the code).


Now we want to create this expression:




So thinking back to yesterday this is pretty easy:


CodeMethodReferenceExpression lhsEquals = new CodeMethodReferenceExpression(lhs, "Equals");

CodeExpression ifTest = new CodeMethodInvokeExpression(lhsEquals, new CodeExpression[] { rhs });


ifTest will be used later when we define the conditional statement.


Next we need to define the true and false statements.  Since we’re going to be calling Console.WriteLine more then once we’ll create a method reference as follows:


CodeExpression console = new CodeTypeReferenceExpression(typeof(System.Console));

CodeMethodReferenceExpression consoleWriteLine = new CodeMethodReferenceExpression(console, "WriteLine");


And create a pair of expressions that invoke Console.WriteLine with the correct string literal – one expression for each boolean result.


CodeExpression ifTrueExp = new CodeMethodInvokeExpression(


                                    new CodeExpression[] {

                                          new CodePrimitiveExpression("This strings are equal")



CodeExpression ifFalseExp = new CodeMethodInvokeExpression(


                                    new CodeExpression[] {

                                          new CodePrimitiveExpression("This strings are not equal")



One gotcha is that the CodeConditionStatement class does not take a collection of CodeExpression objects but rather a collection of CodeStatement objects.  So we need to create CodeStatement derived objects from our CodeExpression objects.  Thankfully the CodeExpressionStatement class is there to help us out.  So not we create statement arrays that will be added to the CodeConditionStatement:


CodeStatement[] ifTrue = { new CodeExpressionStatement(ifTrueExp) };

CodeStatement[] ifFalse = { new CodeExpressionStatement(ifFalseExp) };


And finally we create our conditional statement and add it to our method:


CodeConditionStatement ifStatement = new CodeConditionStatement(ifTest, ifTrue, ifFalse);




Now when we run this code and we render the following:


namespace CodeDOMSample2


    using System;



    public class ConditionalTest



        public virtual void IfSample(string lhs, string rhs)


            if (lhs.Equals(rhs))


                System.Console.WriteLine("This strings are equal");




                System.Console.WriteLine("This strings are not equal");






There you have it.  A generating a conditional expression really isn’t that hard.


As usual – no warranty expressed or implied.  Your mileage may vary.  Please don’t taunt happy fun ball.


Comments (1)

Skip to main content